Independent Iterators
Asked at Google
Reported by the problem's creator, not verified
You are building tee, which splits one stream of values into several independent streams. Tee(source, n) wraps source, a list of integers that you must treat as a one-shot stream: read it at most once, from the start, and read a value only when some iterator needs a value that has not been read yet.
There are n iterators, numbered 0 to n - 1. Each one returns the values of source in order, from the first, and moves forward only when it is asked. The iterators do not affect each other.
Examples
Example 1
Input: init =
[[1,2,3,4],2], operations =[["buffered",[]],["next",[0]],["next",[0]],["buffered",[]],["next",[1]],["buffered",[]],["next",[1]],["buffered",[]],["next",[0]],["next",[0]],["next",[0]],["buffered",[]],["next",[1]],["next",[1]],["next",[1]],["buffered",[]]]Output:
[0,1,2,2,1,1,2,0,3,4,null,2,3,4,null,0]Explanation: Nothing is read at first, so
buffered()is 0. Iterator 0 reads 1 and 2, so 2 values are held until iterator 1 catches up. After iterator 1 reads 1 the gap is 1, and after it reads 2 the gap is 0.