Back to problems

Union Iterator for Two Sorted Iterators (Dedup, Streaming, O(1) Extra Space)

Algorithm · Mongodb · Medium

You are given two streaming iterators, it1 and it2, each of which yields elements in strictly non-decreasing order. The iterators support only forward traversal; you cannot reset them or access elements at arbitrary positions. Implement a class UnionIterator that provides the following two methods: hasNext() — returns true if there is at least one more element available. next() — returns the next smallest element from the combined set formed by the two iterators. If…

Loading the rest of the question…