Design and implement a “Shuffle” feature in a music player

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2022-06-25

Problem Statement

Designing and implementing the Shuffle functionality for a music player application. The system should allow users to play songs in a randomized order while ensuring smooth transitions, user control, and consistency across sessions.


Design and implement the Shuffle feature for a music player, ensuring randomized playback without repetition, smooth control transitions, and extensibility for advanced shuffle algorithms.


🔶 Functional Requirements


1.Shuffle Playback
•When shuffle mode is enabled, songs in the current playlist/album/queue must be played in a random order.
•No song should repeat until all songs have been played (fair shuffle).
•Allow switching between normal (sequential) and shuffle modes anytime.
2.Playback Controls
•Support play, pause, next, previous.
•In shuffle mode, next should pick the next random song from the unplayed pool.
•Previous should allow going back to the last played song.
3.Queue & Persistence
•Maintain a playback queue that updates as songs are played.
•If shuffle mode is toggled off → playback returns to sequential order.
•Persist shuffle state so that users returning to the app continue in the same mode.
4.Optional Extensions
•Weighted shuffle (higher chance for user’s favorite songs).
•Context-aware shuffle (avoid repeating the same artist consecutively).
•Cross-device sync (continue shuffle order across multiple devices).


🔶 Non-Functional Requirements


•Efficiency: Shuffling large playlists (10K+ songs) should be performant.
•Consistency: No duplicates in a shuffle cycle.
•Extensibility: Easy to plug in new shuffle algorithms (e.g., weighted).
•User Experience: Seamless transition between shuffle and sequential modes.


🔶 Constraints


•Shuffle must work for both small (5 songs) and large playlists (10K+ songs).
•Must handle offline mode (no dependency on backend shuffling).
•Edge case: playlists with 1 or 2 songs.


🔶 Example Use Cases


1.User enables shuffle mode on a 50-song playlist → songs are played in random order without repetition until all 50 are played.
2.User presses next repeatedly → each time a random unplayed song is played.
3.User presses previous → last played song is replayed.
4.User disables shuffle mid-way → playback continues from the correct sequential position in the playlist.
5.User closes the app and reopens → shuffle state and order are preserved.


Solution

Comments