KatInterview

Design an In-Memory Music Player and Playlist Engine in Go

System DesignModel answer
HardModel answerSystem Designsystem designgoconcurrency

Last updated

Design an in-memory music player engine in Go supporting playlists, deduplication, list merging, playback operations, and safe concurrent access.

Architect and implement an object-oriented, in-memory audio player component in Go.

Core Features & Design Specs:

  1. Domain Abstractions:
    • Define types for Song, Playlist, and Player.
    • Separate song metadata from playlist track instances so duplicate song occurrences remain individually addressable.
  2. Playlist Operations:
    • Provide methods to append tracks, remove entries by index, and reorder items within a playlist.
    • Support configurable duplicate rules (AllowDuplicates vs RejectDuplicates) along with explicit deduplication.
    • Provide a mechanism to merge two playlists into a new playlist according to policy options (e.g., standard concatenation, unique track filtering, or alternating elements), maintaining item ordering without incurring deadlocks or state contamination.
  3. Playback Control Engine:
    • Implement control workflows: Play, Pause, Next, Previous, SetShuffle, and repeat variations (RepeatOff, RepeatOne, RepeatAll).
    • Decouple hardware playback mechanisms behind an audio driver abstraction (e.g., AudioOutput).
  4. Concurrency & Thread Safety:
    • Guarantee thread safety for simultaneous mutations (inserts, deletes, reordering) while active playback or snapshot processing is underway.
    • Outline your strategy for lock management, memory snapshots, and deadlock prevention.
  5. Complexity & Testing:
    • Document time and space complexities for each operation.
    • Summarize key unit testing scenarios for edge behaviors (empty lists, out-of-bounds indices, repeat loops, shuffle mutations).

Hints

Hint 1

Assign each inserted track a unique incrementing sequence ID within the playlist to distinguish separate insertions of identical songs.

Hint 2

To prevent deadlocks during multi-playlist operations like merging, acquire brief snapshot copies of each source independently rather than locking multiple playlists simultaneously.

Hint 3

Maintain a monotonic version number on playlists. This allows the playback engine to refresh its internal index state lazily when mutations occur.