Started okay with the basic merge logic but the duplicate handling question is where things got interesting.
Start by clarifying requirements: define union semantics (e.g., sorted merge with duplicate handling), then design a lazy iterator that pulls from both sources on demand. Use a heap or two-pointer approach for efficiency, and address infinite iterators by never materializing the full stream.
Pro tip: Mention that you'd use a priority queue to merge k sorted iterators (generalizing to two) and that you'd handle duplicates by either skipping equal elements or tracking the last emitted value—this shows you think about scalability and edge cases.
Ask whether the input iterators are sorted, how duplicates should be handled (keep all, deduplicate, or emit once), and whether infinite iterators are possible. Confirm that hasNext() and next() must be O(1) or O(log n) and that memory should be bounded.
For two sorted iterators, use a two-pointer approach: peek at the next element from each, compare, and emit the smaller. For unsorted or k-way, use a min-heap. Explain that this naturally supports lazy evaluation because you only advance the iterator that produced the emitted element.
Decide on duplicate policy: if deduplicating, track the last emitted value and skip any subsequent equal values from either iterator. If keeping all, simply emit both when equal. Discuss trade-offs: deduplication requires extra state but reduces output size.
Emphasize that the design never exhausts an iterator unless necessary. For infinite iterators, hasNext() should always return true if at least one iterator has a next element. next() should only pull from the iterator(s) needed to produce the next union element, ensuring constant memory.
State time complexity: O(1) per next() for two sorted iterators (amortized), O(log k) for heap-based k-way merge. Space: O(1) for two iterators, O(k) for heap. Cover edge cases: empty iterators, one infinite, both infinite, duplicates at boundaries.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.