Katkat

Implement AudioStream Mixer

TomaSoftware Engineer · Technical Screen

Last updated

This problem tests your ability to manage temporal data and stateful buffers by implementing a class that merges multiple incoming audio streams into a single output track using overwrite-based mixing logic.

Problem Statement

You are building a core component for a real-time audio editor. You need to implement an AudioStreamMixer class that manages audio data across multiple tracks. Each track consists of a series of audio samples represented as integers.

The mixer operates on a shared timeline. You must support writing audio segments to specific tracks and retrieving the final mixed output.

Requirements

  • write(track_id, start_time, samples): Writes a list of samples to a specific track_id, starting at start_time. If samples already exist at that track's position, the new samples overwrite the old ones.
  • get_mixed_output(end_time): Returns the final mixed buffer from time 0 up to end_time. The mixed output is calculated by summing the samples of all tracks present at each time step t. If no track has data at time t, the sample value is 0.

Constraints

  • track_id is an integer.
  • start_time and end_time are non-negative integers.
  • samples is a list of integers.
  • Max time range is 10^6.

Example

# Mixer setup
mixer = AudioStreamMixer()

# Track 1: [10, 20, 30] starting at time 0
mixer.write(1, 0, [10, 20, 30])

# Track 2: [5, 5] starting at time 1
mixer.write(2, 1, [5, 5])

# Output at time 3: [10, 25, 35]
# Explanation: 
# t=0: [10] (T1=10, T2=0)
# t=1: [20+5] = 25
# t=2: [30+5] = 35
print(mixer.get_mixed_output(3)) 

Edge Cases

  • Empty writes or end_time smaller than any written data.
  • Overlapping writes on the same track.
  • Tracks with gaps between them.

Hints

Hint 1

Use a dictionary to map `track_id` to another dictionary (or a sparse array) that maps `time` to `sample_value`.

Hint 2

For the `write` operation, simply update the values in the specific track's dictionary for the duration of the incoming samples.

Hint 3

For `get_mixed_output`, iterate from time 0 to `end_time - 1`, summing the values found at each time index across all active track dictionaries.

Follow-up questions

  • How would you optimize the `get_mixed_output` method if the total duration is extremely large (e.g., hours of audio)?
  • How would you change the implementation if the mix behavior was 'add' instead of 'overwrite'?
  • What data structures would you use if you needed to support a `delete_range(track_id, start, end)` operation?