Sound Track Iterator

Asked at AirbnbPhone screen

Reported by the problem's creator, not verified

A sound track is read in pieces. The class SoundTrack is provided in the starter code: track.read(x, buf) takes up to x characters from where the track last stopped, appends them to the list buf one character per element, and returns how many it appended. A track that has fewer than x characters left appends them all, so a result below x means the track is exhausted, and an exhausted track appends nothing and returns 0. For a track holding "abcde", read(3, buf) appends a, b, c and returns 3, and a following read(2, buf) appends d, e and returns 2.

Build SoundTrackIterator, which plays a list of tracks one after another as a single stream. Its constructor receives the tracks' contents as a list of strings, and the starter already wraps each one in a SoundTrack. Your code must get the characters only by calling read(x, buf) on those SoundTrack objects.

play(x) is provided: it calls your read(x, buf) with a new empty buf and returns [count, characters], the count read returned and the characters it appended.

Examples

  1. Example 1

    Input: init = [["a","bcd","ef"]], operations = [["play",[2]],["play",[1]],["play",[3]]]

    Output: [[2,"ab"],[1,"c"],[3,"def"]]

    Explanation: Two characters come from 'a' and 'bcd'. One more is the 'c' of 'bcd'. The last read asks for 3 and gets 'd' from the rest of 'bcd', then 'ef', so it returns 'def'.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.