Consolidate Half-Open Interval Ranges
Greedy & IntervalsAuto-graded · 19 test casesLast updated
Tests interval merging with half-open semantics, distinguishing overlap from adjacency, and handling nested ranges correctly.
Problem
You are given a collection of half-open intervals representing time ranges. Each interval [start, end) includes the start time but excludes the end time. Your task is to combine all intervals that share at least one overlapping point, and return a sorted list of non-overlapping intervals.
Two intervals [a, b) and [c, d) overlap if and only if they share at least one point in common. Because the intervals are half-open, [1, 3) and [3, 5) do not overlap—the first excludes 3 while the second includes it—so they remain separate in the output.
Write a function consolidate_intervals(intervals) that takes a list of intervals and returns the consolidated result.
Constraints
0 <= len(intervals) <= 200,000- Each interval
[start, end)satisfies0 <= start < end <= 10^12 - The input may be unsorted and may contain duplicate or fully nested intervals
- Output must be sorted by start time and contain no overlapping intervals
Notes
- Adjacent intervals that merely touch at a boundary point should remain separate
- When multiple intervals overlap, the merged interval should span from the earliest start to the latest end among them
- Empty input should return an empty list
Examples
Example 1
Input: consolidate_intervals([[1, 3], [2, 6], [8, 10], [10, 12]])
Output: [[1, 6], [8, 10], [10, 12]]
Hints
Hint 1
Sort the intervals first—once sorted by start time, you only need to check whether each new interval overlaps with the most recent merged one.
Hint 2
For half-open intervals, two intervals [a,b) and [c,d) overlap when c < b (not c <= b). The strict inequality is critical.
Hint 3
When merging overlapping intervals, use max(current_end, new_end) to handle cases where a later interval is completely nested inside an earlier one.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The solution uses a greedy sweep algorithm after sorting the intervals by their start times.
Core insight: Once intervals are sorted by start time, each new interval can only potentially overlap with the most recently merged interval. All earlier merged intervals end before the current merged interval starts, so no new interval (which starts at or after the current start) can reach back to overlap them.
Algorithm steps:
-
Handle the empty case: If the input is empty, return an empty list immediately.
-
Sort by start time: Sort all intervals by their start values. Python's sort naturally handles ties by comparing end values, but the specific tie-breaking order doesn't affect correctness.
-
Initialize with the first interval: Create a result list with a copy of the first sorted interval. We copy to avoid mutating the caller's data.
-
Sweep through remaining intervals: For each interval
[start, end):- Check for overlap: Compare
startwith the end of the last merged interval. Ifstart < merged_end, the intervals overlap (they share at least one point).- Merge: Extend the merged interval's end to
max(merged_end, end). Themaxis crucial—it handles nested intervals where a later interval might end earlier than the current merged end.
- Merge: Extend the merged interval's end to
- No overlap: If
start >= merged_end, there's no overlap (adjacency at the boundary doesn't count). Append the new interval as a separate entry.
- Check for overlap: Compare
-
Return the result: The merged list contains sorted, non-overlapping intervals.
Why the strict inequality matters: With half-open intervals, [1, 3) contains points in [1, 3) but excludes 3. The interval [3, 5) includes 3. Since they don't share any points, we use start < merged_end (strict) rather than start <= merged_end. If start == merged_end, they merely touch and should remain separate.
Why max is necessary: Consider merging [1, 10) with [2, 4). After sorting, we process [2, 4) second. It overlaps (2 < 10), but simply setting the end to 4 would incorrectly shrink the merged interval from 10 to 4. Using max(10, 4) preserves the correct end of 10.
Complexity
Time: O(n log n) for sorting the intervals, plus O(n) for the single-pass sweep through the sorted list. For n = 200,000, this runs efficiently.
Space: O(n) for the sorted list and the output list. Python's Timsort uses up to O(n) auxiliary space internally. We could sort in-place to reduce extra memory, but that would modify the caller's input, which is generally undesirable.
Solution Code
from typing import List
def consolidate_intervals(intervals: List[List[int]]) -> List[List[int]]:
if not intervals:
return []
ordered = sorted(intervals)
merged = [ordered[0][:]]
for start, end in ordered[1:]:
if start < merged[-1][1]:
merged[-1][1] = max(merged[-1][1], end)
else:
merged.append([start, end])
return merged