Back to problems Related: LeetCode 23 (lists) →

K-Way Stream Merge

Heaps • Min-heap of cursors • Watermark & lateness

K sorted streams of timestamped messages. Keep a min-heap of iterators: each entry knows (next_timestamp, stream_id, index). Repeatedly pop the smallest timestamp, emit that message, then push the next element from that stream if any. A running watermark (max timestamp seen) helps flag late arrivals (here ts < watermark − 50). Production code may also evict stale streams using heartbeats (wall-clock); the animation uses a fixed timeline for clarity.

next()O(log K)
InitO(K)
SpaceO(K+N)
Step0/0
Ready
Press Play or Step. Three streams; heap always holds the current head of each non-empty stream.