Spotify

Music Streaming Platform

Design a music player and its playlists: collaborative playlists that stay consistent under concurrent edits, a queue with shuffle, repeat and previous, pluggable shuffle strategies, and event-fed listening history.

Overview

A music streaming app has a catalog of tracks, playlists that users build and share, and a player that plays a queue of tracks with shuffle and repeat. In a low level design interview the scope is the object model: playlists and who may edit them, the player's state and queue, how shuffle and repeat interact, and how listening history is recorded.

The problem tests three things. First, whether you separate the playlist (shared, edited by several people) from the player's queue (a private copy, reordered by shuffle). Second, whether the player's rules are precise: what 'next' does at the end of the queue under each repeat mode, what 'previous' does halfway through a track, and what turning shuffle off restores. Third, whether shared playlists stay consistent when two people edit at once.

A strong answer draws the playlist and the player as separate classes, states each control's rule exactly, makes shuffle a Strategy, and uses a version number so a stale reorder is refused instead of silently applied.

Scoping the problem

Seven questions settle the scope, and several of them have no single right answer, so state your choice out loud.

  • Is the queue the playlist itself, or a copy? This decides what happens when someone edits a playlist while it plays. The usual answer is a copy made at load time.
  • Who may edit a playlist? This sets permissions and concurrency. The owner may, and so may collaborators on a collaborative playlist.
  • Which repeat modes? They change what next does at the end of the queue. The usual set is off, all and one.
  • Does repeat one also hold on a manual skip? This is a frequent edge case. The usual answer is no: repeat one replays only when a track ends by itself. It is a choice to state.
  • What does previous do mid-track? Players differ. A common rule is to restart the track if it is more than 3 seconds in, else go back. This is also a choice to state.
  • What must shuffle restore when turned off? It must keep the user's place: the original order, still on the current track.
  • Search, recommendations, audio streaming, downloads? These are separate systems and out of scope; history is recorded for them.

Functional requirements

  • Playlists: add(user, track) and move(user, src, dst, expected_version); only the owner, or collaborators on a collaborative playlist, may edit
  • Player: load(playlist) copies a snapshot into the queue and starts playing
  • Controls: pause, resume, next, previous, and tick(seconds) for time passing
  • Repeat modes off, all and one; repeat one replays only when a track ends by itself
  • Shuffle on keeps the current track first; shuffle off restores the original order on the same track
  • Shuffle order comes from a pluggable strategy; one strategy plays recently heard tracks later
  • Listeners receive player events; History records every track that starts

Non-functional requirements

  • Concurrent edits never lose a track: the playlist lock guards every change
  • A stale reorder is refused with Conflict, never applied to the wrong track
  • The queue is a copy, so editing a playlist never disturbs what is playing
  • A new shuffle rule is a new ShuffleStrategy; the player does not change
  • Deterministic in tests: time is passed to tick and shuffles take a seeded random generator

Interview questions

  • Users say shuffle is not random: they hear the same songs again. What do you do?
  • Two collaborators reorder the same playlist at the same time. What happens?
  • Someone edits the playlist while I am listening to it. Does my queue change?
  • Where does listening history live, and how do other features use it?

Sign up free to read the full topic

More Low-Level Design topics

Start free on Cariara · Coding problems · System design studio