KatInterview

Design an Audio Player Core with Queue Management and Shuffle Logic

Data Structure DesignModel answer
MediumModel answerData Structure DesignObject-Oriented DesignState MachineData Structure Design

Last updated

Architect the object-oriented structure and state machine for a media player featuring play/pause controls, queue prioritization, looping modes, and deterministic shuffle handling.

Design the core object models, state transitions, and playback policies for a media player application.

Your solution should support the following operations and features:

  1. Core Controls: play(), pause(), and next().
  2. Track Queueing: User-queued tracks that take precedence over normal playlist progression.
  3. Looping Modes: Support for standard playback (stop at end), loop single track, and loop playlist/queue.
  4. Shuffle Playback: Randomization that visits each remaining item without immediate duplication while preserving history for navigation.
  5. Hardware Decoupling: Interfaces that decouple playback logic from audio output devices (allowing for stubbed testing).

Operational Rules & Assumptions

  • Tracks are unique domain objects; identical metadata (e.g., song title) does not imply track identity. A single track instance can appear multiple times in a playlist.
  • Explicitly define queue priority: User "play next" additions must take precedent over the default playlist sequence.
  • State handling must account for async events, such as track completion notifications emitted by the underlying audio driver interface.
  • Threading and network streaming can be simplified to a single-threaded local model, but asynchronous callbacks from the audio hardware driver must be handled safely.

Hints

Hint 1

Consider using a state pattern or explicit state enum (e.g., PLAYING, PAUSED, STOPPED) to govern how commands like play() or next() behave.

Hint 2

Separate the user-queued tracks (priority queue / explicit buffer) from the underlying playlist sequence.

Hint 3

To make shuffle testable, inject a random seed or randomizer dependency into the shuffle logic rather than relying directly on system randomness.