Multi-Stream Round-Robin Iterator

Asked at AirbnbOnsite

Reported by the problem's creator, not verified

You are merging several streams of characters into one. Each stream yields single characters and then runs out; the constructor of MultiStreamIterator receives the streams' contents as a list of strings, and the starter already turns each one into an iterator. A stream can be empty. The starter also provides the abstract class StreamIterator with has_next() and next(), and MultiStreamIterator must implement that interface.

The merged sequence is round robin: take one element from the first stream, then one from the second, and so on to the last, then start again from the first. A stream that has run out is skipped from then on, and the sequence ends when every stream has. For streams abc, de and f it is a d f b e c.

Examples

  1. Example 1

    Input: init = [["abc","de","f"]], operations = [["next",[]],["next",[]],["next",[]],["next",[]],["next",[]],["next",[]],["has_next",[]]]

    Output: ["a","d","f","b","e","c",false]

    Explanation: The first round takes a from abc, d from de and f from f. In the second round f is exhausted and skipped, so b and e follow. In the third only abc is left, so c comes last. After it has_next() is false.

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.