PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates skill in designing stateful data structures, time-indexed array manipulation, and per-track mixing semantics including overwrite behavior across overlapping writes.

  • medium
  • Toma
  • Coding & Algorithms
  • Software Engineer

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]]`

Constraints

  • 1 <= len(track_names) <= 50
  • All track names are distinct
  • 0 <= timestamp, duration
  • Audio sample values may be negative, zero, or positive integers
  • The sum of lengths of all written `data` arrays is at most 200000
  • The sum of all read durations is at most 200000

Examples

Input: (['x'], [('write', 'x', [1, 1, 1, 1], 0), ('write', 'x', [9, 9], 2), ('read', 0, 5)])

Expected Output: [[1, 1, 9, 9, 0]]

Explanation: The second write overwrites times 2 and 3 on track x, so the mixed output is [1, 1, 9, 9, 0].

Input: (['x', 'y'], [('write', 'x', [1, 1, 1, 1], 0), ('write', 'x', [2, 2], 2), ('write', 'y', [9, 9], 2), ('read', 0, 5)])

Expected Output: [[1, 1, 11, 11, 0]]

Explanation: Track x becomes [1, 1, 2, 2] on times 0..3, track y contributes [9, 9] at times 2 and 3, so the sum is [1, 1, 11, 11, 0].

Input: (['a', 'b'], [('write', 'a', [5, -1, 3], 1), ('write', 'b', [2, 2, 2, 2], 0), ('read', 0, 6), ('write', 'a', [4], 2), ('read', 1, 4)])

Expected Output: [[2, 7, 1, 5, 0, 0], [7, 6, 5, 0]]

Explanation: The first read mixes both tracks over six timestamps. The later write changes only track a at time 2 from -1 to 4, affecting the second read.

Input: (['m'], [('write', 'm', [], 3), ('read', 2, 0), ('read', 0, 3)])

Expected Output: [[], [0, 0, 0]]

Explanation: Writing empty data changes nothing. A zero-duration read returns an empty list, and reading unwritten timestamps returns zeros.

Hints

  1. Store each track separately. Overwrite behavior only matters within the same track.
  2. A hash map from timestamp to sample value makes writes easy: later writes simply assign over earlier ones.
Last updated: Apr 19, 2026

Related Coding Questions

  • Approximate Pi from Random Points - Toma (easy)
  • Approximate Pi from Random Points - Toma (easy)

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.