Playlist Management with Doubly Linked List

Quick Overview

Design a playlist class backed by a doubly linked list that appends tracks, removes a track by id, and moves a current-track pointer forward and back, all in constant time. It tests pointer bookkeeping at both ends of the list, where the pointer goes when the current track is removed, and empty-playlist edge cases.

Playlist Management with Doubly Linked List

Company: Sonos

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a `Playlist` data structure backed by a doubly linked list that manages an ordered collection of tracks identified by unique integer ids. The playlist keeps a current track pointer that updates with each navigation operation. New tracks are appended to the end of the playlist. Removing a track by its id detaches it from the list in `O(1)` time. If the current track is removed, the pointer advances to the next track, or falls back to the previous track if there is no next one. All operations must run in `O(1)` time. On an empty playlist no current track is set, and both `playNext` and `playPrevious` return `-1`. When the current pointer is at the last track, `playNext` returns `-1` (end of playlist); similarly, `playPrevious` returns `-1` when the pointer is at the first track. A navigation call that returns `-1` leaves the pointer where it was. Implement the following methods: - `addTrack(trackId: int)`: Append a new track with the given id to the end of the playlist. If the playlist was empty, the current track pointer is set to this new track. - `removeTrack(trackId: int)`: Remove the track with the given id from the playlist in `O(1)` time. If it was the current track, advance the pointer to the next track, or fall back to the previous track if there is no next. - `playNext() -> int`: Advance the current pointer to the next track and return its id. Returns `-1` if the playlist is empty or there is no next track. - `playPrevious() -> int`: Move the current pointer to the previous track and return its id. Returns `-1` if the playlist is empty or there is no previous track. Example 1: ```text Input: ["Playlist", "addTrack", "addTrack", "addTrack", "playNext", "playNext", "playPrevious", "removeTrack", "playNext"] [[], [10], [20], [30], [], [], [], [20], []] Output: [null, null, null, null, 20, 30, 20, null, -1] Explanation: playlist = Playlist() # empty playlist, current = None playlist.addTrack(10) # list: [10*], current = 10 playlist.addTrack(20) # list: [10*, 20], current = 10 playlist.addTrack(30) # list: [10*, 20, 30], current = 10 playlist.playNext() # -> 20 (current moves 10 -> 20) playlist.playNext() # -> 30 (current moves 20 -> 30) playlist.playPrevious() # -> 20 (current moves 30 -> 20) playlist.removeTrack(20) # current was 20, advances to 30; list: [10, 30*] playlist.playNext() # -> -1 (current = 30 is the tail, no next) ``` Example 2: ```text Input: ["Playlist", "playNext", "playPrevious", "addTrack", "playPrevious", "addTrack", "playNext", "removeTrack", "playPrevious"] [[], [], [], [5], [], [7], [], [5], []] Output: [null, -1, -1, null, -1, null, 7, null, -1] Explanation: playlist = Playlist() # empty playlist playlist.playNext() # -> -1 (playlist is empty) playlist.playPrevious() # -> -1 (playlist is empty) playlist.addTrack(5) # list: [5*], current = 5 playlist.playPrevious() # -> -1 (current = 5 is the head, no prev) playlist.addTrack(7) # list: [5*, 7], current = 5 playlist.playNext() # -> 7 (current moves 5 -> 7) playlist.removeTrack(5) # 5 is not current; list: [7*], current = 7 playlist.playPrevious() # -> -1 (current = 7 is the head, no prev) ``` ```hint Finding a track by id A linked list on its own needs a scan to find a node by its id. Think about what you could keep next to the list to reach any node directly. ``` ```hint Removing the current track Decide where the current pointer goes before you clear the removed node's links, and check what happens to `head` and `tail` when the removed track sits at either end. ``` ### Constraints & Assumptions - `1 <= trackId <= 10^9` - All `trackId` values added are unique: an id is never added twice unless it was removed in between. - At most `10^5` calls in total across all methods. - `removeTrack` is only called with a `trackId` that is currently in the playlist. ### What a Strong Answer Covers - The supporting structure that makes `removeTrack` run in `O(1)`, and why the linked list alone cannot do it. - How `addTrack` sets `head`, `tail` and `current` on an empty playlist, including one that was emptied by removals. - How unlinking updates the neighbors and the `head` and `tail` pointers, including removal of the only track. - Where the current pointer goes when the current track is removed, and when that update happens relative to clearing the node's links. - Navigation at either end and on an empty playlist, with the pointer left unchanged when a call returns `-1`. ### Follow-up Questions - How would you add a repeat mode where `playNext` on the last track moves to the first track? - How would you support inserting a track right after a given track, or moving a track to a new position, and still keep every operation `O(1)`? - How would you add shuffle playback while keeping the original order available? - What changes if a UI thread and a background sync thread call these methods at the same time?

Overview: Design a playlist class backed by a doubly linked list that appends tracks, removes a track by id, and moves a current-track pointer forward and back, all in constant time. It tests pointer bookkeeping at both ends of the list, where the pointer goes when the current track is removed, and empty-playlist edge cases.

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

Design a Playlist data structure backed by a doubly linked list that manages an ordered collection of tracks identified by unique integer ids.

The playlist keeps a current track pointer that updates with each navigation operation. New tracks are appended to the end of the playlist. Removing a track by its id detaches it from the list in O(1) time. If the current track is removed, the pointer advances to the next track, or falls back to the previous track if there is no next one.

All operations must run in O(1) time. On an empty playlist no current track is set, and both playNext and playPrevious return -1. When the current pointer is at the last track, playNext returns -1 (end of playlist); similarly, playPrevious returns -1 when the pointer is at the first track. A navigation call that returns -1 leaves the pointer where it was.

Implement the following methods:

  • addTrack(trackId: int) : Append a new track with the given id to the end of the playlist. If the playlist was empty, the current track pointer is set to this new track.
  • removeTrack(trackId: int) : Remove the track with the given id from the playlist in O(1) time. If it was the current track, advance the pointer to the next track, or fall back to the previous track if there is no next.
  • playNext() -> int : Advance the current pointer to the next track and return its id. Returns -1 if the playlist is empty or there is no next track.
  • playPrevious() -> int : Move the current pointer to the previous track and return its id. Returns -1 if the playlist is empty or there is no previous track.

Example 1:

Input:
["Playlist", "addTrack", "addTrack", "addTrack", "playNext", "playNext", "playPrevious", "removeTrack", "playNext"]
[[],         [10],       [20],       [30],       [],         [],         [],             [20],          []]

Output: [null, null, null, null, 20, 30, 20, null, -1]

Explanation:
playlist = Playlist()              # empty playlist, current = None
playlist.addTrack(10)              # list: [10*], current = 10
playlist.addTrack(20)              # list: [10*, 20], current = 10
playlist.addTrack(30)              # list: [10*, 20, 30], current = 10
playlist.playNext()                # -> 20   (current moves 10 -> 20)
playlist.playNext()                # -> 30   (current moves 20 -> 30)
playlist.playPrevious()            # -> 20   (current moves 30 -> 20)
playlist.removeTrack(20)           # current was 20, advances to 30; list: [10, 30*]
playlist.playNext()                # -> -1   (current = 30 is the tail, no next)

Example 2:

Input:
["Playlist", "playNext", "playPrevious", "addTrack", "playPrevious", "addTrack", "playNext", "removeTrack", "playPrevious"]
[[],         [],         [],             [5],        [],             [7],        [],         [5],           []]

Output: [null, -1, -1, null, -1, null, 7, null, -1]

Explanation:
playlist = Playlist()              # empty playlist
playlist.playNext()                # -> -1   (playlist is empty)
playlist.playPrevious()            # -> -1   (playlist is empty)
playlist.addTrack(5)               # list: [5*], current = 5
playlist.playPrevious()            # -> -1   (current = 5 is the head, no prev)
playlist.addTrack(7)               # list: [5*, 7], current = 5
playlist.playNext()                # -> 7    (current moves 5 -> 7)
playlist.removeTrack(5)            # 5 is not current; list: [7*], current = 7
playlist.playPrevious()            # -> -1   (current = 7 is the head, no prev)

Constraints & Assumptions

  • 1 <= trackId <= 10^9
  • All trackId values added are unique: an id is never added twice unless it was removed in between.
  • At most 10^5 calls in total across all methods.
  • removeTrack is only called with a trackId that is currently in the playlist.

What a Strong Answer Covers Guidance

  • The supporting structure that makes removeTrack run in O(1) , and why the linked list alone cannot do it.
  • How addTrack sets head , tail and current on an empty playlist, including one that was emptied by removals.
  • How unlinking updates the neighbors and the head and tail pointers, including removal of the only track.
  • Where the current pointer goes when the current track is removed, and when that update happens relative to clearing the node's links.
  • Navigation at either end and on an empty playlist, with the pointer left unchanged when a call returns -1 .

Follow-up Questions Guidance

  • How would you add a repeat mode where playNext on the last track moves to the first track?
  • How would you support inserting a track right after a given track, or moving a track to a new position, and still keep every operation O(1) ?
  • How would you add shuffle playback while keeping the original order available?
  • What changes if a UI thread and a background sync thread call these methods at the same time?