how to efficiently merge int ranges in a stream?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Efficiently merging integer ranges in a stream depends on one crucial detail: whether the incoming ranges are already ordered by start value. If they are sorted, you can merge online with constant extra state. If they are not sorted, exact merging generally requires buffering or another ordering structure.
Define the merge rule first
Most interval-merging tasks combine ranges when they overlap, and some also combine ranges that touch.
Examples:
- overlap merge:
[1, 3]and[2, 5]become[1, 5] - touching merge:
[1, 3]and[4, 6]become[1, 6]if adjacency counts as mergeable
Decide that rule before coding. Otherwise, a correct implementation for one use case becomes wrong for another.
Best case: sorted stream
If the stream arrives sorted by range start, you only need to keep the current merged interval and compare each new interval against it.
This is the ideal streaming solution because it needs only one active interval plus the current input.
Why unsorted streams are harder
If a later range can begin earlier than a previously seen range, exact online merging becomes harder. For example, after seeing [10, 12], a later [1, 20] changes everything.
That means an unsorted stream generally needs one of these:
- buffer then sort
- maintain an ordered structure such as a tree map
- accept approximate or windowed results
There is no simple one-pass constant-memory exact solution for arbitrary unsorted interval streams.
Batch-then-sort strategy
If you can afford buffering, sorting is still the standard exact approach.
This is often the right answer unless the stream is truly unbounded.
Event-time windows for long streams
In stream-processing systems, a common compromise is to merge only within a bounded window. For example, you might collect intervals for one minute, sort and merge them, then emit the result. That gives deterministic results with bounded memory, at the cost of not performing a global merge across all time.
This is usually the practical answer in systems such as telemetry or log processing where exact infinite-history merging is not realistic anyway.
Implementation notes in C++
The same sorted-stream logic is easy to express in C++:
This assumes the input is already sorted. If that assumption is false, sort first or document the contract strictly.
Common Pitfalls
The most common mistake is assuming an unsorted stream can be merged exactly with only one active range in memory. Another is forgetting to define whether touching ranges should merge, which changes results around boundaries such as [3, 5] and [6, 8]. Developers also often sort by the wrong key or skip sorting entirely in the batch case. Off-by-one mistakes are common when integer adjacency is supposed to count as overlap. Finally, some implementations emit partial results too early in a real stream and later discover that a delayed range should have merged with something already sent downstream.
Summary
- If ranges arrive sorted by start, you can merge them online with constant extra state.
- If the stream is unsorted, exact merging usually requires buffering or an ordered data structure.
- Decide whether adjacent integer ranges should merge before implementing the algorithm.
- Sorting plus a linear scan remains the standard exact solution for batched input.
- Windowed merging is often the practical compromise for real unbounded streams.
- Make the ordering assumption explicit in the API or code comments so callers do not misuse the function.
Related reading
- how to efficiently move data from Kafka to an Impala table?
- How to enable GC logging for Apache Kafka brokers, while preventing log file overwrites and capping disk space usage
- How to enable Kafka logging with log4j
- How to enable remote JMX on Kafka brokers (for JmxTool)?
- How to efficiently pagination and sort data from multiple services?
- How to efficiently search in an ordered matrix?
- How to enable batch inserts with Hibernate and Spring Boot
- How to enable native resolution for apps on iPhone 6 and 6 Plus?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.