Implement a Reader That Streams Characters Across a List of Sound Tracks

Quick Overview

An object-oriented coding question: given sound track classes that share a base read method, build an iterator whose read call returns the next characters across a list of tracks. It tests programming against an interface, crossing track boundaries, short reads and edge cases.

Implement a Reader That Streams Characters Across a List of Sound Tracks

Company: Airbnb

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Technical Screen

You are given several sound track classes that are already implemented. All of them inherit from a base class `Sound`, which defines a method `read(x, s)`: it reads up to `x` characters of the track's data into the buffer `s` and advances the track's position, so the next call continues where the previous one stopped. For example, if a track's data is `"abcde"`, then `read(3, s)` puts `"abc"` into `s`, and a following `read(2, s)` puts `"de"` into `s`. Implement a class `SoundTrackIterator` that is built from a list of tracks, which may be instances of different subclasses, and provides its own `read(x, s)`. It reads the next `x` characters from the tracks in list order, continuing from one track into the next when a track runs out. For tracks whose data are `"a"`, `"bcd"` and `"ef"`: ```text read(2, s) -> s receives "ab" read(1, s) -> s receives "c" read(3, s) -> s receives "def" ``` In the interview the signature was written as `read(x int, s string)`. In Python, you can model the buffer as a list of characters: ```python class Sound: def read(self, x: int, s: list[str]) -> int: """Provided by each subclass. Put up to x characters of this track into s.""" raise NotImplementedError class SoundTrackIterator: def __init__(self, tracks: list[Sound]): ... def read(self, x: int, s: list[str]) -> int: ... ``` ```hint Only the base method Think about what the iterator may assume about a track when all it can see is the base class's `read`. ``` ```hint When to move on Ask what tells you that the current track is finished, and whether getting fewer characters than you asked for is enough evidence. ``` ### Clarifying Questions - What does the provided `read` return: the number of characters written, nothing, or the data itself? How does a caller detect that a track has ended? - Does `read` write from the start of the buffer, overwriting it, or append to what is already there? - Can a track return fewer characters than requested before it has ended, for example if it decodes its data in blocks? - What should the iterator return once every track is exhausted, and can `x` be zero or negative? - Can tracks be added to the list after reading has started, and must the iterator be safe for several threads? ### What a Strong Answer Covers - Settling the `read` contract (return value, buffer semantics, end-of-track signal) before coding - Programming against the base class, so that every subclass works without special cases - Correct reads across track boundaries, including one request that spans several tracks, and empty tracks - Handling short reads correctly, and stopping cleanly when every track is exhausted - A walk-through of the example and tests for edge cases such as `x = 0`, an empty list and reads that end exactly at a boundary - Time proportional to the characters read plus the tracks passed over ### Follow-up Questions - How would you add a `skip(n)` or `seek` operation to the iterator? - If reading a track is slow (from disk or the network), how would you prefetch the next track? - How would you support tracks being appended while a reader is active? - How would you make the iterator safe for two threads reading at the same time?

Overview: An object-oriented coding question: given sound track classes that share a base read method, build an iterator whose read call returns the next characters across a list of tracks. It tests programming against an interface, crossing track boundaries, short reads and edge cases.

Read the full Airbnb Software Engineer interview experience this question came from

Airbnb logo
Airbnb
Oct 30, 2026
mediumSoftware EngineerTechnical ScreenSoftware Engineering Fundamentals
1
0

You are given several sound track classes that are already implemented. All of them inherit from a base class Sound, which defines a method read(x, s): it reads up to x characters of the track's data into the buffer s and advances the track's position, so the next call continues where the previous one stopped. For example, if a track's data is "abcde", then read(3, s) puts "abc" into s, and a following read(2, s) puts "de" into s.

Implement a class SoundTrackIterator that is built from a list of tracks, which may be instances of different subclasses, and provides its own read(x, s). It reads the next x characters from the tracks in list order, continuing from one track into the next when a track runs out.

For tracks whose data are "a", "bcd" and "ef":

read(2, s)  ->  s receives "ab"
read(1, s)  ->  s receives "c"
read(3, s)  ->  s receives "def"

In the interview the signature was written as read(x int, s string). In Python, you can model the buffer as a list of characters:

class Sound:
    def read(self, x: int, s: list[str]) -> int:
        """Provided by each subclass. Put up to x characters of this track into s."""
        raise NotImplementedError


class SoundTrackIterator:
    def __init__(self, tracks: list[Sound]):
        ...

    def read(self, x: int, s: list[str]) -> int:
        ...

Clarifying Questions Guidance

  • What does the provided read return: the number of characters written, nothing, or the data itself? How does a caller detect that a track has ended?
  • Does read write from the start of the buffer, overwriting it, or append to what is already there?
  • Can a track return fewer characters than requested before it has ended, for example if it decodes its data in blocks?
  • What should the iterator return once every track is exhausted, and can x be zero or negative?
  • Can tracks be added to the list after reading has started, and must the iterator be safe for several threads?

What a Strong Answer Covers Guidance

  • Settling the read contract (return value, buffer semantics, end-of-track signal) before coding
  • Programming against the base class, so that every subclass works without special cases
  • Correct reads across track boundaries, including one request that spans several tracks, and empty tracks
  • Handling short reads correctly, and stopping cleanly when every track is exhausted
  • A walk-through of the example and tests for edge cases such as x = 0 , an empty list and reads that end exactly at a boundary
  • Time proportional to the characters read plus the tracks passed over

Follow-up Questions Guidance

  • How would you add a skip(n) or seek operation to the iterator?
  • If reading a track is slow (from disk or the network), how would you prefetch the next track?
  • How would you support tracks being appended while a reader is active?
  • How would you make the iterator safe for two threads reading at the same time?