Start by clarifying the requirements: lazy merge of two sorted iterables with deduplication, handling infinite streams and long runs of duplicates efficiently. Then outline a generator-based solution using iterators and a lookahead buffer, emphasizing O(1) memory and linear time. Finally, demonstrate laziness with a minimal example and provide unit tests that verify both correctness and lazy evaluation.
Pro tip: Use a small lookahead buffer to avoid quadratic behavior on long runs of equal elements, and explicitly test laziness by using infinite iterators and asserting that only the needed elements are consumed.
Restate the problem: merge two non-decreasing iterables lazily, remove duplicates, handle infinite inputs, and avoid quadratic behavior on long runs of equal elements. Mention that only yield and built-ins are allowed.
Use iterators for both inputs and maintain a lookahead value for each. At each step, compare the current values, yield the smaller one if it's new (not equal to the last yielded), and advance the corresponding iterator. Handle exhaustion gracefully.
Write the merge_unique function using a while loop and yield. Use a variable to track the last yielded value to skip duplicates. Ensure that only one element is consumed from each iterator at a time to maintain laziness.
Show that the generator only consumes elements as needed. For example, use an infinite iterator (like itertools.count) and demonstrate that taking the first few elements works without hanging.
Include tests for: merging finite sorted lists with duplicates, handling empty inputs, merging infinite streams (using islice), and verifying that the generator does not pre-buffer by checking consumption counts.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.