I went straight to the min-heap approach which was the right call, but I fumbled the part about exhausted streams longer than I should have.
Use a min-heap to track the current head of each stream, initialized with the first element from each non-empty stream. On each next() call, pop the smallest element, then push the next element from the same stream if available. This yields O(log k) time per call, where k is the number of streams, and handles empty or exhausted streams by skipping them during initialization and after each pop.
Pro tip: Mention that you can optimize by storing the stream index and value in the heap, and that you should handle the case where a stream is empty initially by not adding it to the heap. Also, discuss the trade-off between using a heap versus a tournament tree for very large k.
Ask about the number of streams, typical sizes, and whether streams can be empty or become exhausted. Confirm that next() should throw an exception if no elements remain.
Choose a min-heap (priority queue) to store the current head of each stream. Each heap entry contains the value and the stream index (or reference) to know which stream to advance.
Iterate through all streams, and for each that has at least one element (hasNext() true), call next() and push the value and stream index into the heap. Skip empty streams.
For next(): if heap is empty, throw exception. Pop the smallest entry, then if that stream still has elements, push its next value. Return the popped value. For hasNext(): return true if heap is not empty.
Time per next() is O(log k) for heap operations, where k is the number of streams. Space is O(k). Discuss handling of empty streams (skipped at init) and streams that exhaust mid-way (no push after pop).
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.