Consolidate Overlapping Ranges
Greedy & IntervalsAuto-graded · 25 test casesLast updated
Tests interval merging logic, correctly handling touching boundaries, nested ranges, and producing sorted disjoint output.
You receive a collection of closed ranges defined by integer boundaries. Each range may appear in any order. Your task is to consolidate all ranges that share at least one common point, then return the consolidated ranges in ascending order by their starting point.
Function Signature
def consolidate_ranges(ranges: list[list[int]]) -> list[list[int]]:
Each element ranges[i] = [start, end] represents a closed range including both start and end.
Rules
- Two ranges share a common point when at least one integer belongs to both. For instance,
[1, 4]and[4, 5]both contain the integer 4, so they must be consolidated. Conversely,[1, 2]and[3, 4]have no integer in common and remain separate. - Consolidation applies transitively: if range A shares a point with B, and B shares a point with C, then all three collapse into one range, regardless of whether A and C directly overlap.
- When multiple ranges consolidate, the result spans from the minimum starting point to the maximum ending point across the entire group.
- The input may include duplicate ranges or ranges fully contained within others.
- Output the consolidated ranges in ascending order by starting point. Because consolidated ranges never overlap, this ordering is unambiguous.
Constraints
1 <= len(ranges) <= 10^40 <= start <= end <= 10^9for every range
Examples
Example 1
Input: consolidate_ranges([[2, 5], [9, 12], [4, 7], [13, 15]])
Output: [[2, 7], [9, 12], [13, 15]]
Example 2
Input: consolidate_ranges([[6, 8], [1, 6]])
Output: [[1, 8]]
Example 3
Input: consolidate_ranges([[5, 7], [1, 2], [3, 4], [2, 3], [9, 9]])
Output: [[1, 4], [5, 7], [9, 9]]
Hints
Hint 1
What happens to overlapping ranges after you sort them by their starting point?
Hint 2
Once sorted, ranges that need consolidation will appear consecutively—you can process them in a single pass.
Hint 3
Remember that closed ranges touching at a single endpoint must merge, so use <= when comparing boundaries.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The core insight is that sorting the ranges by their starting point transforms the problem into a single linear scan. After sorting, any group of ranges that need consolidation will appear consecutively in the sorted sequence.
Maintain a result list where the last element represents the current consolidated range being built. Process each sorted range:
- If the current range's start is at most the end of the last consolidated range (meaning they share at least one point), extend the consolidated range's endpoint to accommodate the new range. The new endpoint is the maximum of the two endpoints.
- If the current range starts strictly after the last consolidated range ends (no shared points), finalize the previous range and begin a new consolidated range.
The comparison uses <= rather than < because ranges are closed on both ends. The ranges [1, 4] and [4, 5] both include the integer 4, making them overlapping.
Why This Works
After sorting, if two ranges overlap, the one with the smaller start appears first. Since we process in ascending start order, any range that overlaps with the current consolidated group must have a start that falls within or touches that group's span. This means:
- All ranges in a consolidation group appear consecutively after sorting
- Each range either extends the current group or starts a new one
- We never need to look backward or make multiple passes
Transitive consolidation happens automatically. When [1,2], [2,3], and [3,4] arrive in order, each one extends the current group's endpoint, allowing the next to attach.
Complexity
Time: O(n log n) dominated by sorting. The consolidation pass runs in O(n).
Space: O(n) for the sorted copy and the output. Python's sort uses O(n) auxiliary space in the worst case.
Solution
def consolidate_ranges(ranges: list[list[int]]) -> list[list[int]]:
result = []
for start, end in sorted(ranges):
if result and start <= result[-1][1]:
if end > result[-1][1]:
result[-1][1] = end
else:
result.append([start, end])
return result
The implementation details:
- Sorting automatically orders by start, with end as a tiebreaker
- We create fresh lists when appending to avoid mutating the input
- The conditional extension
if end > result[-1][1]ensures we never shrink a consolidated range when encountering a fully contained range - The
start <= result[-1][1]condition correctly handles ranges that touch at a single point
For the input [[1,2],[2,3],[3,4],[5,7],[9,9]] after sorting:
- Start with
[1,2] [2,3]touches at 2, extend to[1,3][3,4]touches at 3, extend to[1,4][5,7]starts at 5 > 4, begin new range[9,9]starts at 9 > 7, begin new range- Result:
[[1,4],[5,7],[9,9]]