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
Coding & Algorithms
Software Engineer
Apple
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

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
heaps
top-k
hash maps
sorting
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample 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...


Submit Your Answer
Markdown supported