Implement AudioStream Mixer
Company: Toma
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Implement an `AudioStream` class that mixes multiple named audio tracks on an integer timeline.
Requirements:
- The constructor receives a fixed list of valid track names.
- `write(trackName, data, timestamp)` writes an audio clip onto one track.
- `timestamp >= 0`
- `data[i]` applies to time `timestamp + i`
- If `trackName` is not one of the known tracks, throw an error.
- If multiple writes overlap on the **same track**, the **most recent** `write` call overrides earlier values for the overlapping timestamps.
- The mixed output at any timestamp is the **sum across all tracks** after applying the per-track overwrite rule.
- Missing values contribute `0`.
- `read(timestamp, duration)` should return an array of length `duration`, representing the mixed audio samples from `timestamp` through `timestamp + duration - 1`.
Example 1:
- Create a stream with one track: `x`
- `write("x", [1, 1, 1, 1], 0)` sets track `x` to `1` at times `0,1,2,3`
- `write("x", [9, 9], 2)` overwrites times `2,3` on track `x`
- `read(0, 5)` should return `[1, 1, 9, 9, 0]`
Example 2:
- Create a stream with tracks `x` and `y`
- `write("x", [1, 1, 1, 1], 0)`
- `write("x", [2, 2], 2)` so track `x` becomes `1,1,2,2` on times `0,1,2,3`
- `write("y", [9, 9], 2)` so track `y` contributes `9,9` at times `2,3`
- Then `read(0, 5)` should return `[1, 1, 11, 11, 0]`
Design and implement the class correctly, including initialization of internal track storage and the `read` method.
Quick Answer: This question evaluates skill in designing stateful data structures, time-indexed array manipulation, and per-track mixing semantics including overwrite behavior across overlapping writes.
Design the behavior of an `AudioStream` that mixes multiple named audio tracks on an integer timeline.
For this judge, implement a function `solution(track_names, operations)` that simulates the class and returns the results of all `read` operations.
Rules:
- The stream is created with a fixed list of valid track names.
- Each operation is processed in order.
- A write operation has the form `('write', trackName, data, timestamp)`.
- `timestamp >= 0`
- `data[i]` is written at time `timestamp + i`
- If `trackName` is not in the known track list, raise `ValueError`
- If multiple writes overlap on the same track, the most recent write overrides earlier values for overlapping timestamps.
- A read operation has the form `('read', timestamp, duration)`.
- It returns the mixed audio from `timestamp` to `timestamp + duration - 1`
- Missing values contribute `0`
- The mixed value at each timestamp is the sum across all tracks after applying per-track overwrite rules
- Return a list containing the result of every `read` operation, in order.
Example:
- tracks = `['x']`
- write `('write', 'x', [1, 1, 1, 1], 0)`
- write `('write', 'x', [9, 9], 2)`
- read `('read', 0, 5)`
- output: `[[1, 1, 9, 9, 0]]`