Locate Uncovered Ranges Between Intervals
Greedy & IntervalsAuto-graded · 24 test casesLast updated
Tests interval merging, sorting, and boundary logic to identify all missing integer ranges within the overall coverage span.
You are given a list of integer intervals intervals, where each interval is a pair [start, end] with both endpoints inclusive and start <= end.
Your task is to identify all uncovered ranges of integers that lie between the smallest and largest values covered by the union of all intervals.
Specifically:
- Merge all overlapping or adjacent intervals into a union of disjoint covered ranges.
- Let
minCoveredbe the smallest integer in any interval, andmaxCoveredbe the largest. - Find every maximal contiguous range of integers in
[minCovered, maxCovered]that is not covered by any interval.
Return these uncovered ranges as a list of disjoint intervals [gapStart, gapEnd] (inclusive), sorted in ascending order by gapStart.
Important: Two intervals like [1, 3] and [4, 6] are considered adjacent (they share no gap) because there is no integer strictly between 3 and 4.
Constraints
1 <= len(intervals) <= 200000- Each interval endpoint is a 32-bit signed integer.
intervals[i][0] <= intervals[i][1]for alli.
Examples
Example 1
Input: locate_uncovered_ranges([[1, 3], [7, 10], [2, 4]])
Output: [[5, 6]]
Example 2
Input: locate_uncovered_ranges([[1, 2], [3, 5]])
Output: []
Example 3
Input: locate_uncovered_ranges([[10, 12], [1, 3], [6, 8]])
Output: [[4, 5], [9, 9]]
Hints
Hint 1
Sort the intervals by their starting points to process them in order from left to right.
Hint 2
Track the rightmost covered endpoint as you sweep through the sorted intervals; a gap exists when the next interval starts more than one unit away.
Hint 3
Remember that endpoints are inclusive integers—intervals [a, b] and [b+1, c] touch with no gap between them.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem is a variation of the classic interval merging pattern. Instead of returning the merged intervals themselves, we return the gaps between them.
Algorithm:
-
Sort the intervals by their starting points. This allows us to process them left-to-right in a single pass.
-
Initialize tracking: Start with
current_endset to the endpoint of the first interval. This represents the rightmost integer currently covered by the merged block we're building. -
Sweep through remaining intervals: For each interval
[start, end]:- Gap detection: If
start > current_end + 1, there's at least one uncovered integer between the current merged block and this interval. Record the gap[current_end + 1, start - 1], then begin a new merged block withcurrent_end = end. - Merging: Otherwise, the interval overlaps or touches the current block. Extend the coverage:
current_end = max(current_end, end).
- Gap detection: If
-
Return the collected gaps.
Why the + 1 matters: Since endpoints are inclusive integers, intervals [1, 3] and [4, 6] leave no gap (3 and 4 are consecutive). A gap only exists when start > current_end + 1, meaning at least integer current_end + 1 is uncovered.
Correctness: After processing each interval, current_end represents the maximum endpoint seen so far among intervals that merge into the current block. When we detect a gap, no future interval can fill it (they all start at or after the current start), and no prior interval filled it (their coverage is captured in current_end). This makes each reported gap both genuine and maximal.
Why sorting ensures correct output: Processing intervals in sorted order guarantees that gaps are discovered and emitted in ascending order, producing a naturally sorted output with no post-processing needed.
Complexity
- Time: O(n log n) — dominated by sorting; the sweep itself is O(n).
- Space: O(n) — for the sorted copy and output list. In the worst case (all intervals disjoint and non-adjacent), the output contains n−1 gaps.
Solution Code
from typing import List
def locate_uncovered_ranges(intervals: List[List[int]]) -> List[List[int]]:
ordered = sorted(intervals)
gaps = []
current_end = ordered[0][1]
for start, end in ordered[1:]:
if start > current_end + 1:
gaps.append([current_end + 1, start - 1])
current_end = end
else:
current_end = max(current_end, end)
return gaps