As reported: Coinbase IC4 tech execution round (India, early 2025): one problem in 4 levels, each level revealed only after the previous one is finished.
All iterators share one interface: hasNext() tells whether another value exists, next() returns it and advances. Calling hasNext() any number of times must not skip values.
Level 1 — alternating iterator over lists. Given n lists, return their values alternately: the first value of each list in order, then the second value of each, and so on, skipping lists that have run out.
lists = [[0, 1, 2], [], [3, 4], [5]] -> 0, 3, 5, 1, 4, 2
Level 2 — range iterator. RangeIterator(start, end, step) returns start, start + step, … up to and including end when it is hit exactly; the step may be negative.
RangeIterator(0, 10, 2) -> 0, 2, 4, 6, 8, 10
RangeIterator(5, -4, -3) -> 5, 2, -1, -4
Level 3 — list iterator. ListIterator(values) returns the values of one list in order, e.g. [0, 1, 2, 3, 4, 5] → 0, 1, 2, 3, 4, 5.
Level 4 — alternate between iterators. Change the level 1 class so it takes a list of iterator objects — any mix of ListIterator and RangeIterator — and returns their values alternately with the same rules.
InterleavingIterator([ListIterator([1, 2, 3]), RangeIterator(10, 4, -3), ListIterator([])])
-> 1, 10, 2, 7, 3, 4
Level 1 is close to LeetCode 281 — Zigzag Iterator, with any number of lists.
Constraints: at most 10^3 iterators; at most 2 · 10^5 values in total; values and range bounds fit in 32-bit integers, and so does every value a range produces.
Clarifications
stepis never 0. A range that moves away fromend(e.g.RangeIterator(0, 10, -1)) is empty; one withstart == endreturns juststart.- If
endis not hit exactly, the range stops at the last value before passing it:RangeIterator(0, 10, 3)→0, 3, 6, 9. next()is only called whenhasNext()is true.
Code runner format: the first line is the number of iterators k; each of the next k lines is list m v1 … vm or range start end step. The harness builds a ListIterator / RangeIterator for each line, wraps them in InterleavingIterator and prints every value it returns, separated by spaces (- if there are none). It calls hasNext() twice before each next().