Design a music streaming app with playlists, per-user playback and a top songs chart
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a music streaming app, similar to Spotify, that supports user accounts, a catalog of songs, user-created playlists, and per-user playback control.
Each user has their own independent playback session. The catalog is shared across all users. Songs can be added to playlists and played individually or as part of a playlist. The app also tracks the play count of every song to power a global top songs chart.
All identifiers (`userId`, `songId`, `playlistId`) are unique positive integers. Operations on non-existent entities return sensible defaults (`False`, `-1`, or an empty list) as described per method.
Implement a `MusicStreamingApp` class with these methods:
- `registerUser(userId: int, name: str) -> bool`: Register a new user. Returns `True` if successful, or `False` if `userId` already exists.
- `addSong(songId: int, title: str, artist: str, duration: int) -> bool`: Add a song to the global catalog. Returns `True` if successful, or `False` if `songId` already exists.
- `createPlaylist(playlistId: int, userId: int, name: str) -> bool`: Create a new playlist owned by `userId`. Returns `True` on success, `False` if `playlistId` exists or `userId` is unknown.
- `addSongToPlaylist(playlistId: int, songId: int) -> bool`: Append a song to the end of a playlist. Returns `True` on success, `False` if either id is unknown.
- `playSong(userId: int, songId: int) -> bool`: Start playing a single song for the given user. Clears any active playlist context and increments the song's play count. Returns `True` on success, `False` if either id is unknown.
- `playPlaylist(userId: int, playlistId: int) -> bool`: Start playing the playlist from its first song for the given user and increment that song's play count. Returns `True` on success, `False` if the playlist is unknown or empty, or the user is unknown.
- `nextSong(userId: int) -> int`: Advance to the next song in the user's current playlist and play it (incrementing its play count). Returns the new current `songId`, or `-1` if no playlist is active or the playlist is exhausted.
- `currentSong(userId: int) -> int`: Return the `songId` the user is currently playing, or `-1` if the user is not playing anything.
- `topSongs(k: int) -> List[int]`: Return the `k` most played `songId`s in descending order of play count, breaking ties by smaller `songId` first.
When `nextSong` moves past the last song of the playlist, playback stops: that call returns `-1`, `currentSong` returns `-1` afterwards, and later `nextSong` calls return `-1` until the user starts a song or a playlist again. A song that has never been played has a play count of 0 and still takes part in the chart. If the catalog holds fewer than `k` songs, `topSongs` returns all of them.
```hint Session state
List every way a user's session can change (a single song, the start of a playlist, a next track, the end of a playlist) and write down which fields each change sets or clears. Stale playlist context is the usual source of wrong answers here.
```
```hint Chart cost
`topSongs` needs at most 100 ids, while play counts only move up by one at a time. Work out what a chart computed on demand costs under the operation limit, and what it would take to keep the chart ordered as plays happen.
```
### Examples
#### Example 1
```python
app = MusicStreamingApp() # empty service
app.registerUser(1, "Alice") # True (user 1 created)
app.addSong(10, "Yesterday", "Beatles", 125) # True (song 10 added)
app.addSong(11, "Imagine", "Lennon", 183) # True (song 11 added)
app.playSong(1, 10) # True (Alice plays song 10, count = 1)
app.currentSong(1) # 10 (Alice's current track)
app.topSongs(2) # [10, 11] (10 has 1 play, 11 has 0)
```
Song 11 has never been played, but it still fills the second slot of the chart.
#### Example 2
```python
app = MusicStreamingApp()
app.registerUser(1, "Alice") # True
app.registerUser(2, "Bob") # True
app.registerUser(1, "Alicia") # False (user 1 already exists)
app.addSong(10, "Yesterday", "Beatles", 125) # True
app.addSong(11, "Imagine", "Lennon", 183) # True
app.addSong(12, "Hey Jude", "Beatles", 431) # True
app.createPlaylist(100, 1, "Morning") # True (owned by Alice)
app.createPlaylist(101, 3, "Ghost") # False (user 3 is unknown)
app.addSongToPlaylist(100, 11) # True
app.addSongToPlaylist(100, 12) # True
app.addSongToPlaylist(100, 99) # False (song 99 is unknown)
app.playPlaylist(2, 100) # True (Bob hears 11, plays of 11 = 1)
app.nextSong(2) # 12 (plays of 12 = 1)
app.playSong(1, 12) # True (plays of 12 = 2)
app.playSong(1, 10) # True (plays of 10 = 1)
app.nextSong(1) # -1 (Alice has no active playlist)
app.nextSong(2) # -1 (Bob's playlist is exhausted)
app.currentSong(2) # -1 (Bob's playback has stopped)
app.currentSong(1) # 10
app.topSongs(3) # [12, 10, 11]
```
Bob's playlist has two songs, so his second `nextSong` runs past the end and stops his playback. Song 12 has two plays (Bob, then Alice), and songs 10 and 11 tie at one play each, so the smaller id 10 comes before 11.
### Constraints & Assumptions
- `1 <= userId, songId, playlistId <= 10^6`
- `1 <= duration <= 10^4` (seconds)
- At most `10^5` operations in total across all methods.
- Song titles, artist names and playlist names are non-empty strings of length at most 100.
- `1 <= k <= 100` for `topSongs(k)`.
### Clarifying Questions to Ask
- Can any user play a playlist that another user created, or only its owner?
- Can the same song appear in one playlist more than once?
- If a song is appended to a playlist while someone is playing it, should their `nextSong` reach it?
- How often is `topSongs` called compared with plays, and does the chart need to be exact after every play?
### What a Strong Answer Covers
- An entity model that separates the shared catalog and playlists from each user's playback session.
- Data structures that keep registration, playlist edits and playback at constant time.
- Correct session transitions for `playSong`, `playPlaylist`, `nextSong` and playlist exhaustion, with every play counted once.
- A `topSongs` implementation with the required tie-break and a cost estimate under the operation limit.
- The specified return values for unknown ids, duplicate ids and empty playlists.
### Follow-up Questions
- How would you add `previousSong` and a shuffle mode without changing the order other users hear?
- If `topSongs` were called after nearly every play, how would you change the design?
- How would you serve a chart of the most played songs over the last 7 days?
- What changes if requests for different users are handled by several threads at once?
Overview: Design an in-memory music streaming service with user accounts, a shared song catalog, playlists and an independent playback session for each user. It tests object modeling, state transitions during playlist playback, and keeping a top-k most played chart with a deterministic tie-break.