Song Play Analytics
Learn this problemProblem statement
Implement a small music analytics service. It stores songs, records users listening to songs, ranks songs by unique listeners, and tracks each user's recently played unique songs.
Operations
Process operations in order and return one row for each operation that produces output.
["ADD_SONG", songName]: Add a song and return its positive integer ID as a one-element row. IDs start at1and increase by1.["PLAY", songId, userId]: Record that the user played the song. This operation produces no output.["ANALYTICS"]: Return one row containing every song formatted assongName(uniqueListeners). Sort by unique-listener count descending, then song name lexicographically ascending.["RECENT", userId, k]: Return up tokdistinct song names most recently played by that user, newest first.
Repeated plays by the same user count once toward a song's unique-listener total. For RECENT, replaying a song moves it to the front rather than creating a duplicate.
Interviewer follow-ups
The base exercise adds songs, records plays, and prints analytics by unique listeners. Interviewers then ask candidates to return the last three, or more generally the last k, unique songs played by one user while preserving recency.
Function
runSongAnalytics(operations: String[][]) → String[][]Examples
Example 1
operations = [["ADD_SONG","Song A"],["ADD_SONG","Song B"],["ADD_SONG","Song C"],["PLAY","1","u1"],["PLAY","1","u2"],["PLAY","2","u1"],["PLAY","2","u1"],["ANALYTICS"],["RECENT","u1","3"]]return = [["1"],["2"],["3"],["Song A(2)","Song B(1)","Song C(0)"],["Song B","Song A"]]Song A has two unique listeners. User u1's repeated Song B play moves Song B to the front but does not duplicate it.
Constraints
- Song names are unique.
- Every
PLAYreferences an existing song ID. 1 <= k <= 100000
More Rippling problems
- Delivery Cost TrackerPHONE SCREEN · Seen Jul 2026
- Corporate Card Expense RulesPHONE SCREEN · Seen Jun 2026
- Camel CardsPHONE SCREEN · Seen May 2026
- Article Vote TrackerPHONE SCREEN · Seen May 2026
- Employee Resource Access ManagementONSITE INTERVIEW · Seen Jan 2026
- Limit an Organization Tree's HeightONSITE INTERVIEW · Seen Aug 2025
- Distributed System RecoveryOA · Seen Jul 2025
- Server Upgrade PlanningOA · Seen Jul 2025