Design a song playlist tracker ranked by listen count with name tie-breaks

Quick Overview

Design a class that tracks listen counts for songs, removes dropped songs along with their history, and returns the most and least listened songs with ties broken by name. It tests keeping a hash map and an ordered structure in sync, handling ties at both ends of the order, and stating the real cost of each operation.

Design a song playlist tracker ranked by listen count with name tie-breaks

Company: Roblox

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a song playlist tracker that maintains songs ordered by their listen frequency. The system tracks how many times each song has been listened to and supports adding listens, removing songs, and querying the most or least popular songs. When multiple songs share the same listen count, ties are broken by **lexicographic order** (smallest name first). Songs that are dropped are completely removed from the playlist along with their listen history. Implement a class `SongPlaylist` with the following methods: - `listen(song: str) -> None`: Increment the listen count of the given song by 1. If the song is not in the playlist, add it with a count of 1. - `drop(song: str) -> bool`: Remove the song from the playlist entirely. Return `True` if the song existed and was removed, `False` otherwise. - `getListenCount(song: str) -> int`: Return the listen count of the given song, or 0 if the song is not in the playlist. - `getMostListened() -> str`: Return the song with the highest listen count. If multiple songs are tied, return the lexicographically smallest name. Return an empty string if the playlist is empty. - `getLeastListened() -> str`: Return the song with the lowest listen count. If multiple songs are tied, return the lexicographically smallest name. Return an empty string if the playlist is empty. - `getPlaylistSize() -> int`: Return the number of distinct songs currently in the playlist. Example 1, with each call followed by its return value: ```text listen("b") -> None listen("a") -> None listen("b") -> None getMostListened() -> "b" getLeastListened() -> "a" listen("a") -> None getMostListened() -> "a" drop("a") -> True drop("a") -> False getListenCount("a") -> 0 getPlaylistSize() -> 1 ``` After the third call "b" has 2 listens and "a" has 1. Once "a" also reaches 2, the two songs tie and "a" wins because it is the smaller name. Dropping "a" removes it, so the second drop returns `False` and its count reads 0. Example 2: ```text getMostListened() -> "" listen("song9") -> None listen("song10") -> None getLeastListened() -> "song10" listen("song9") -> None listen("song9") -> None getMostListened() -> "song9" drop("song9") -> True listen("song9") -> None getListenCount("song9") -> 1 getMostListened() -> "song10" getPlaylistSize() -> 2 ``` Names compare character by character, so "song10" comes before "song9". After the drop, "song9" starts again at 1 listen and ties with "song10", which wins the tie. ```hint Two jobs, two structures A hash map answers count lookups at once but cannot tell you the minimum or maximum. Think about a second structure ordered by the pair (count, name), and what has to happen to it each time a count changes. ``` ```hint Ties at the top If that structure is sorted ascending by (count, name), its last element has the highest count but the largest name among the songs tied at that count. Work out how to reach the first song of that group instead. ``` ### Constraints & Assumptions - `1 <= song.length <= 100` - Song names consist of lowercase English letters and digits. - At most ${10^4}$ total calls will be made. - `getMostListened` and `getLeastListened` return `""` if the playlist is empty. - `getListenCount` returns `0` if the song is not in the playlist. ### Clarifying Questions to Ask - Is the tie-break plain character order, so that "song10" sorts before "song9"? - Can a listen count ever go down, or does it only grow by one per `listen`? - Which calls dominate: listens, or most and least listened queries? - Will the class be called from more than one thread? ### What a Strong Answer Covers - How per-song counts and an order by (count, name) stay consistent on every `listen` and `drop`. - How `getMostListened` finds the smallest name among the songs tied at the highest count. - The edge cases: empty playlist, unknown song, dropping a missing song, and a song listened to again after it was dropped. - The real time cost of each operation for the chosen structure, including the cost of inserting into or deleting from an array. - Working code that matches the given method signatures. ### Follow-up Questions - How would you add `getTopK(k)`, returning the k most listened songs in rank order? - What changes if a listen can add an arbitrary weight, or if counts can decrease? - Without the name tie-break, could every operation run in `O(1)`? How? - How would you make the tracker safe to call from several threads?

Overview: Design a class that tracks listen counts for songs, removes dropped songs along with their history, and returns the most and least listened songs with ties broken by name. It tests keeping a hash map and an ordered structure in sync, handling ties at both ends of the order, and stating the real cost of each operation.

Roblox logo
Roblox
Nov 24, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a song playlist tracker that maintains songs ordered by their listen frequency. The system tracks how many times each song has been listened to and supports adding listens, removing songs, and querying the most or least popular songs.

When multiple songs share the same listen count, ties are broken by lexicographic order (smallest name first). Songs that are dropped are completely removed from the playlist along with their listen history.

Implement a class SongPlaylist with the following methods:

  • listen(song: str) -> None : Increment the listen count of the given song by 1. If the song is not in the playlist, add it with a count of 1.
  • drop(song: str) -> bool : Remove the song from the playlist entirely. Return True if the song existed and was removed, False otherwise.
  • getListenCount(song: str) -> int : Return the listen count of the given song, or 0 if the song is not in the playlist.
  • getMostListened() -> str : Return the song with the highest listen count. If multiple songs are tied, return the lexicographically smallest name. Return an empty string if the playlist is empty.
  • getLeastListened() -> str : Return the song with the lowest listen count. If multiple songs are tied, return the lexicographically smallest name. Return an empty string if the playlist is empty.
  • getPlaylistSize() -> int : Return the number of distinct songs currently in the playlist.

Example 1, with each call followed by its return value:

listen("b")            -> None
listen("a")            -> None
listen("b")            -> None
getMostListened()      -> "b"
getLeastListened()     -> "a"
listen("a")            -> None
getMostListened()      -> "a"
drop("a")              -> True
drop("a")              -> False
getListenCount("a")    -> 0
getPlaylistSize()      -> 1

After the third call "b" has 2 listens and "a" has 1. Once "a" also reaches 2, the two songs tie and "a" wins because it is the smaller name. Dropping "a" removes it, so the second drop returns False and its count reads 0.

Example 2:

getMostListened()        -> ""
listen("song9")          -> None
listen("song10")         -> None
getLeastListened()       -> "song10"
listen("song9")          -> None
listen("song9")          -> None
getMostListened()        -> "song9"
drop("song9")            -> True
listen("song9")          -> None
getListenCount("song9")  -> 1
getMostListened()        -> "song10"
getPlaylistSize()        -> 2

Names compare character by character, so "song10" comes before "song9". After the drop, "song9" starts again at 1 listen and ties with "song10", which wins the tie.

Constraints & Assumptions

  • 1 <= song.length <= 100
  • Song names consist of lowercase English letters and digits.
  • At most 104{10^4} total calls will be made.
  • getMostListened and getLeastListened return "" if the playlist is empty.
  • getListenCount returns 0 if the song is not in the playlist.

Clarifying Questions to Ask Guidance

  • Is the tie-break plain character order, so that "song10" sorts before "song9"?
  • Can a listen count ever go down, or does it only grow by one per listen ?
  • Which calls dominate: listens, or most and least listened queries?
  • Will the class be called from more than one thread?

What a Strong Answer Covers Guidance

  • How per-song counts and an order by (count, name) stay consistent on every listen and drop .
  • How getMostListened finds the smallest name among the songs tied at the highest count.
  • The edge cases: empty playlist, unknown song, dropping a missing song, and a song listened to again after it was dropped.
  • The real time cost of each operation for the chosen structure, including the cost of inserting into or deleting from an array.
  • Working code that matches the given method signatures.

Follow-up Questions Guidance

  • How would you add getTopK(k) , returning the k most listened songs in rank order?
  • What changes if a listen can add an arbitrary weight, or if counts can decrease?
  • Without the name tie-break, could every operation run in O(1) ? How?
  • How would you make the tracker safe to call from several threads?