Design a Music Player
🎵 What is a music player? It is the part of an app like Spotify that decides what plays next. You pick a playlist and press play. The player walks through the songs, lets you skip forward and back, repeats one song or the whole list, and can shuffle the order without losing your place.
The music player looks like a list with an index, and that is exactly why interviewers like it. The first version takes five minutes. Then the follow-ups arrive: what happens past the last song, what "previous" means on the first song, what shuffle does to the song that is playing right now, and what happens if someone edits the playlist while it plays. Each one tests whether your model has the right pieces, or whether you are patching a list index with if statements.
Requirements
"Design a music player like Spotify. Users have playlists, they can play one, skip forward and back, turn on repeat and shuffle."
The prompt names four features. None of them is fully defined, so the questions below pin each one down.
Clarifying Questions
You: What does the player hold? Do we model users, accounts and streaming?
Interviewer: One user's player. A library of songs and some named playlists. No accounts, no audio, no network.
You: What repeat modes are there, and what do they do at the end of the playlist?
Interviewer: Off, repeat one and repeat all. With repeat off, playback stops after the last song. Repeat all goes back to the first song. Repeat one plays the same song again when it ends.
You: With repeat one on, what happens when the user presses next?
Interviewer: They clearly want a different song, so move on. Repeat one only applies when a song finishes on its own.
You: What does previous do on the first song?
Interviewer: With repeat all or repeat one, wrap to the last song. With repeat off, just restart the first song.
You: When the user turns shuffle on in the middle of a song, does that song stop?
Interviewer: No. It keeps playing, and the rest of the playlist is shuffled to come after it. Turning shuffle off also keeps the current song and carries on in the normal order from there.
You: If the user adds a song to a playlist while it is playing, does it join the current session?
Interviewer: Keep it simple: the session works from a copy. The new song shows up the next time they press play.
You: Shuffle is random. How do we test it?
Interviewer: Give the player a seed, and use a fixed algorithm so the order is the same every run.
The repeat-one question is the one candidates skip, and it changes the code. "Song ended" and "user pressed next" are two different events. If you model them as one method, repeat one either traps the user on one song forever or does nothing at all.
💡 Interview tip: When a feature has a mode, ask what each mode does at the boundaries: the first item, the last item, and an empty list. Most bugs in this problem live at the two ends of the playlist, not in the middle.
Final Requirements
- The library holds songs, each with a unique id and a title. Playlists have unique names and an ordered list of songs; a song may appear more than once.
- Playing a playlist starts a session from a copy of it. Later edits to the playlist do not change the running session.
nextmoves to the next song. Past the last one, repeat all and repeat one wrap to the first song, and repeat off stops playback.- When a song ends on its own, repeat one plays it again; otherwise it behaves like
next. prevmoves back one song. On the first song, repeat all and repeat one wrap to the last song, and repeat off restarts the first song.- Shuffle can be turned on and off at any time. Turning it on keeps the current song playing and shuffles the rest after it. Turning it off keeps the current song and continues in playlist order. Shuffle is driven by a seeded generator so the order is reproducible.
- Anyone can ask for the current song and the next few songs in the queue.
Out of scope
- Audio playback, streaming, downloads and song durations.
- Users, accounts, sharing and collaborative playlists.
- Recommendations, radio and "smart shuffle".
- A separate "add to queue" list (we come back to it in Extensibility).