MongoDBCodingFree preview
Union of Two Sorted Iterators
Implement a UnionIterator over two ascending-sorted iterators exposing hasNext()/next() (sometimes named getNext()). Output must be globally ascending, read lazily one element at a time, and in most versions de-duplicated across the two inputs. Follow-up: generalize to K sorted iterators without materializing the data (K-way merge with a heap). Inputs are strictly sorted and may hold integers or strings.
SWE
iterator
merge
deduplication
heap
medium
Frequency
Medium
Last asked
2026-10-13
Stage
phone-screen · tech-screen
Problem
Implement a UnionIterator class that takes two iterators, each already sorted in ascending order, and exposes a single iterator over their union in globally ascending order.
class UnionIterator:
def __init__(self, it1, it2): ...
def has_next(self) -> bool: ... # is there another element?
def next(self) -> int: ... # return the current smallest element and advance the underlying iterator- Method names vary by language (
has_next/next,hasNext/next,hasNext/getNext). - Lazy evaluation is required. Pull from an underlying iterator only when needed; draining both, sorting and replaying is ruled out.
- Duplicates across the two iterators must be removed — the output contains no repeated values.
- Minority variant: some versions state only the merge-in-order requirement with no mention of de-duplication — clarify before coding.
- In the fullest version, inputs are strictly sorted and elements may be integers or strings, so keep the comparison generic.
Examples
Iterator 1: 2, 4, 6, 8, 10
Iterator 2: 1, 2, 3, 5, 8, 13
Output: 1, 2, 3, 4, 5, 6, 8, 10, 13
Iterator 1: apple, banana, orange, tree
Iterator 2: apple, tree, zoo
Output: apple, banana, orange, tree, zoo
Iterator 1: -10, -9, -5, -1, 0, 2, 4
Iterator 2: -9, -5, -3, -2, 0, 1, 2
Output: -10, -9, -5, -3, -2, -1, 0, 1, 2, 4
Iterator 1: 1, 2, 3
Iterator 2: (empty)
Output: 1, 2, 3Pitfalls
- The bar is high on code quality, speed and test coverage; candidates with a working two-head solution have still been rejected. Write tidy code and walk through tests yourself.
has_next()must not consume elements. Keep the "peeked" head separate from the underlying iterator so repeatedhas_next()calls are idempotent.- De-duplication has to cover equal values that appear at the same time on both sides, and (depending on the clarified spec) repeats within one iterator.
- It is not a linked-list merge: an iterator can't be inspected without advancing it, so cached-head bookkeeping is the core of the problem.
- With strictly sorted inputs, the equal-heads check alone de-duplicates; the last-emitted guard matters only if one input can repeat.
- Exhaustion edge cases: one iterator empty from the start, one running out mid-stream, and
next()called when nothing is left. - In C++ the problem plays out differently from common prep write-ups; practice in your interview language.
Follow-ups
Preparation
- Write the two-iterator version with peek caching and de-duplication in your interview language in under 15 minutes, then write its tests (empty inputs, all-equal inputs, interleaved inputs, one side exhausted).
- Extend it to K iterators with a min-heap and state the per-call complexity.
- Practice explaining
has_nextidempotency and why buffering everything violates the lazy-read requirement.
Was this article helpful?
Comments
Sign in to join the discussion
Loading...
