Top K Most Played Songs From an Apple Music Play Log
Last updated: November 29, 2026
Quick Overview
Given a list of song ids from a play log and an integer k, return the k most played songs ordered by play count descending, breaking ties by song id ascending.
Apple
November 29, 20260
0
0 solved
Given a list of song ids from a play log and an integer k, return the k most played songs ordered by play count descending, breaking ties by song id ascending.
Charts and Replay-style summaries are Apple Music staples, so top-k with tie-breaking is a natural screen. The interviewer checks counting plus a heap or bucket approach and correct, deterministic tie handling.
What the Interviewer Expects
- Count plays with a hash map
- Use a size-k heap or nlargest with a composite key
- Get tie-breaking exactly right
- Compare with bucket sort for O(n)
- Handle k larger than the number of distinct songs
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- The log is too large for one machine; compute global top k across shards.
- Return top k per country in one pass.
- Maintain top k over a sliding 24-hour window.
- Use an approximate method with bounded memory for a firehose of plays.
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: list plays of string ids, int k >= 0. Output: list of up to k ids, highest count first, ties by id ascending. If k exceeds distinct songs, return all of them....
High-Level Design
Count with a dictionary in O(n). Then select the top k distinct songs. heapq.nsmallest with key (-count, id) maintains a heap of size k internally and returns results already sorted, so ordering a...