Design a few operations on a Playlist
Difficulty: EASYCategory: LLDFrequency: 1Asked On 2025-06-26
Problem Statement
Design and implement a system to manage music playlists, allowing users to create, modify, and interact with their playlists. The system should be modular, follow good object-oriented design principles, and be easily testable and extensible.
🎯 Functional Requirements
Implement the following core operations:
1.createPlaylist(String playlistId, String name)
•Creates a new playlist with a unique ID and name.
2.addSongToPlaylist(String playlistId, Song song)
•Adds a song to the end of the given playlist.
3.removeSongFromPlaylist(String playlistId, String songId)
•Removes a song by ID from the playlist.
4.getNextSong(String playlistId)
•Returns the next song in the playlist (based on play order).
•Advance the pointer for the next call.
5.shufflePlaylist(String playlistId) (Optional/Advanced)
•Randomizes the order of songs.
6.repeatPlaylist(String playlistId) (Optional)
•If enabled, once the playlist ends, it restarts from the beginning.
🧱 Suggested Class Design
Song
•String songId
•String title
•String artist
•Duration duration
Playlist
•String playlistId
•String name
•List<Song> songs
•int currentIndex
•boolean repeatEnabled
•Queue<Song> shuffleQueue (optional)
PlaylistService
•Manages playlist creation and all playlist operations
🔐 Non-Functional Requirements
•Playlist operations should be thread-safe if used in concurrent environments.
•Follow Single Responsibility and Open/Closed principles.
•Ensure proper input validation and error handling (e.g., invalid playlist or song IDs).
🧪 Sample Test Scenario
1text
2CopyEdit
3createPlaylist("p1", "Workout Mix")
4addSongToPlaylist("p1", Song("s1", "Lose Yourself", "Eminem"))
5addSongToPlaylist("p1", Song("s2", "Stronger", "Kanye West"))
6getNextSong("p1") → "Lose Yourself"
7getNextSong("p1") → "Stronger"
8getNextSong("p1") → null (or "Lose Yourself" if repeat is enabled)✅ Evaluation Criteria
•Code correctness: All operations work as expected.
•Design quality: Follows SOLID principles and object-oriented design.
•Extensibility: Easy to add support for features like shuffle, loop, playlists per user.
•Testability: Design supports unit testing of playlist logic.
•Time management: Efficient discussion, even if stretched.
✨ Optional Extensions
•Add playlist sharing
•Add playlist metadata (cover image, genre, description)
•Store user history of played songs
•Save playlists to file or database