Design a playlist data structure with add, remove and a sorted shuffle

Quick Overview

Design an in-memory playlist that adds and removes songs by integer ID, ignores duplicate adds and missing removes, and returns every song in ascending order. It tests the choice of container, the cost of each operation, and the trade-off between sorting on read and keeping songs ordered on write.

Design a playlist data structure with add, remove and a sorted shuffle

Company: Netflix

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: easy

Interview Round: Other

Design an in-memory playlist data structure that supports adding songs, removing songs, and returning a shuffled order of all songs currently in the playlist. Each song is identified by a unique integer `songId`. Adding a `songId` that already exists in the playlist should be ignored. Removing a `songId` that does not exist should also be ignored. The shuffle operation returns all current songs sorted in ascending order to ensure deterministic results. Implement the following methods: - `add(songId: int)`: Add a song with the given `songId` to the playlist. If the `songId` already exists, the call is ignored. - `remove(songId: int)`: Remove the song with the given `songId` from the playlist. If the `songId` does not exist, the call is ignored. - `shuffle() -> List[int]`: Return a list of all song IDs currently in the playlist in sorted ascending order. Example 1: ```text Input: ["Playlist", "add", "add", "add", "shuffle", "remove", "shuffle", "add", "shuffle"] [[], [1], [2], [3], [], [2], [], [2], []] Output: [null, null, null, null, [1, 2, 3], null, [1, 3], null, [1, 2, 3]] Explanation: playlist = Playlist() # empty playlist playlist.add(1) # playlist: {1} playlist.add(2) # playlist: {1, 2} playlist.add(3) # playlist: {1, 2, 3} playlist.shuffle() # -> [1, 2, 3] (all songs sorted) playlist.remove(2) # playlist: {1, 3} playlist.shuffle() # -> [1, 3] (song 2 was removed) playlist.add(2) # playlist: {1, 2, 3} playlist.shuffle() # -> [1, 2, 3] (song 2 re-added) ``` Example 2: ```text Input: ["Playlist", "add", "add", "remove", "shuffle", "remove", "shuffle"] [[], [5], [5], [5], [], [5], []] Output: [null, null, null, null, [], null, []] Explanation: playlist = Playlist() # empty playlist playlist.add(5) # playlist: {5} playlist.add(5) # playlist: {5} (duplicate ignored) playlist.remove(5) # playlist: {} (song 5 removed) playlist.shuffle() # -> [] (empty playlist) playlist.remove(5) # playlist: {} (song 5 not found, ignored) playlist.shuffle() # -> [] (still empty) ``` ```hint Where to pay for the ordering Find a structure that makes membership changes cheap, then decide whether the ascending order should be kept up to date on every write or produced only when `shuffle` is called. ``` ### Constraints & Assumptions - `1 <= songId <= 10^5` - At most `10^4` calls are made to `add`, `remove` and `shuffle` combined. - `shuffle` returns songs sorted in ascending order for deterministic output. ### Clarifying Questions to Ask - Is the ascending order only a testing convenience, with a real random shuffle expected later? - What is the expected mix of calls? If `shuffle` runs far more often than `add` and `remove`, keeping the songs ordered on every write may be cheaper overall. - May the caller modify the list that `shuffle` returns, so that it has to be a copy of the playlist's state? ### What a Strong Answer Covers - The container chosen for the song IDs and how it turns a repeated add and a missing remove into no-ops. - The time and space cost of each method, including where the cost of producing ascending order is paid. - The trade-off between sorting on read and keeping the songs ordered on write, tied to the expected call mix. - The edge cases from the examples: duplicate adds, removing an absent song, re-adding a removed song, and an empty playlist. ### Follow-up Questions - How would you change `shuffle` to return a uniformly random permutation, and how would you test it once the output is no longer deterministic? - How would you return one uniformly random song in average O(1) time while keeping `add` and `remove` at average O(1)? - Song IDs are bounded by `10^5`. How could you use that bound to produce the ascending list without a comparison sort, and when would that be slower than sorting? - How would you make the playlist safe when several threads call it at once?

Overview: Design an in-memory playlist that adds and removes songs by integer ID, ignores duplicate adds and missing removes, and returns every song in ascending order. It tests the choice of container, the cost of each operation, and the trade-off between sorting on read and keeping songs ordered on write.

Netflix logo
Netflix
Nov 24, 2026
easySoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design an in-memory playlist data structure that supports adding songs, removing songs, and returning a shuffled order of all songs currently in the playlist.

Each song is identified by a unique integer songId. Adding a songId that already exists in the playlist should be ignored. Removing a songId that does not exist should also be ignored.

The shuffle operation returns all current songs sorted in ascending order to ensure deterministic results.

Implement the following methods:

  • add(songId: int) : Add a song with the given songId to the playlist. If the songId already exists, the call is ignored.
  • remove(songId: int) : Remove the song with the given songId from the playlist. If the songId does not exist, the call is ignored.
  • shuffle() -> List[int] : Return a list of all song IDs currently in the playlist in sorted ascending order.

Example 1:

Input:
["Playlist", "add", "add", "add", "shuffle", "remove", "shuffle", "add", "shuffle"]
[[],         [1],   [2],   [3],   [],        [2],      [],        [2],   []]

Output: [null, null, null, null, [1, 2, 3], null, [1, 3], null, [1, 2, 3]]

Explanation:
playlist = Playlist()            # empty playlist
playlist.add(1)                  # playlist: {1}
playlist.add(2)                  # playlist: {1, 2}
playlist.add(3)                  # playlist: {1, 2, 3}
playlist.shuffle()               # -> [1, 2, 3]   (all songs sorted)
playlist.remove(2)               # playlist: {1, 3}
playlist.shuffle()               # -> [1, 3]      (song 2 was removed)
playlist.add(2)                  # playlist: {1, 2, 3}
playlist.shuffle()               # -> [1, 2, 3]   (song 2 re-added)

Example 2:

Input:
["Playlist", "add", "add", "remove", "shuffle", "remove", "shuffle"]
[[],         [5],   [5],   [5],      [],        [5],      []]

Output: [null, null, null, null, [], null, []]

Explanation:
playlist = Playlist()            # empty playlist
playlist.add(5)                  # playlist: {5}
playlist.add(5)                  # playlist: {5}   (duplicate ignored)
playlist.remove(5)               # playlist: {}    (song 5 removed)
playlist.shuffle()               # -> []           (empty playlist)
playlist.remove(5)               # playlist: {}    (song 5 not found, ignored)
playlist.shuffle()               # -> []           (still empty)

Constraints & Assumptions

  • 1 <= songId <= 10^5
  • At most 10^4 calls are made to add , remove and shuffle combined.
  • shuffle returns songs sorted in ascending order for deterministic output.

Clarifying Questions to Ask Guidance

  • Is the ascending order only a testing convenience, with a real random shuffle expected later?
  • What is the expected mix of calls? If shuffle runs far more often than add and remove , keeping the songs ordered on every write may be cheaper overall.
  • May the caller modify the list that shuffle returns, so that it has to be a copy of the playlist's state?

What a Strong Answer Covers Guidance

  • The container chosen for the song IDs and how it turns a repeated add and a missing remove into no-ops.
  • The time and space cost of each method, including where the cost of producing ascending order is paid.
  • The trade-off between sorting on read and keeping the songs ordered on write, tied to the expected call mix.
  • The edge cases from the examples: duplicate adds, removing an absent song, re-adding a removed song, and an empty playlist.

Follow-up Questions Guidance

  • How would you change shuffle to return a uniformly random permutation, and how would you test it once the output is no longer deterministic?
  • How would you return one uniformly random song in average O(1) time while keeping add and remove at average O(1)?
  • Song IDs are bounded by 10^5 . How could you use that bound to produce the ascending list without a comparison sort, and when would that be slower than sorting?
  • How would you make the playlist safe when several threads call it at once?