Find gaps between intervals
Problem Description
You are provided with a list of integer intervals, intervals, where each interval is represented as [start, end]. It is guaranteed that start <= end for all intervals, and the intervals are inclusive.
Your primary task is to identify and return all "gaps" or missing ranges between the covered portions of the number line. Two intervals are considered to "touch" if there is no missing integer between them (e.g., [1,3] and [4,6] touch because 3 and 4 are consecutive integers).
More precisely, the process involves:
- Determining the union of all given intervals.
- Identifying
minCoveredas the smallest integer covered by any interval in the union, andmaxCoveredas the largest integer covered by any interval in the union. - Returning a list of all maximal missing integer ranges within the overall span
[minCovered, maxCovered]that are not covered by the union.
The output should be a list of disjoint intervals [gapStart, gapEnd] (inclusive), sorted by gapStart.
Example
Input: [[1,3],[7,10],[2,4]]
- The covered union is
[1,4] ∪ [7,10]. - The overall span
[minCovered, maxCovered]is[1,10]. - The gaps within
[1,10]are[[5,6]]. Output:[[5,6]]
Notes and Edge Cases
- If the union of intervals is already contiguous (meaning there are no gaps), an empty list
[]should be returned. - Overlapping intervals should be merged and not create gaps.
- The input
intervalslist may be unsorted.
Constraints
- The number of intervals
len(intervals)will be between 1 and 2 * 10^5. - Interval endpoints will fit within a 32-bit signed integer.
AI interview prep
Core dilemma
The central engineering conflict is efficiently merging potentially overlapping or 'touching' integer intervals into a minimal set of disjoint covered ranges, and then accurately identifying all maximal gaps *between* these merged ranges within the overall span defined by the minimum and maximum covered points. The precise definition of 'touching' (e.g., [1,3] and [4,6] touch) is a critical detail that modifies standard interval merging algorithms.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Find gaps between intervals
Problem Description
You are provided with a list of integer intervals, intervals, where each interval is represented as [start, end]. It is guaranteed that start <= end for all intervals, and the intervals are inclusive.
Your primary task is to identify and return all "gaps" or missing ranges between the covered portions of the number line. Two intervals are considered to "touch" if there is no missing integer between them (e.g., [1,3] and [4,6] touch because 3 and 4 are consecutive integers).
More precisely, the process involves:
- Determining the union of all given intervals.
- Identifying
minCoveredas the smallest integer covered by any interval in the union, andmaxCoveredas the largest integer covered by any interval in the union. - Returning a list of all maximal missing integer ranges within the overall span
[minCovered, maxCovered]that are not covered by the union.
The output should be a list of disjoint intervals [gapStart, gapEnd] (inclusive), sorted by gapStart.
Example
Input: [[1,3],[7,10],[2,4]]
- The covered union is
[1,4] ∪ [7,10]. - The overall span
[minCovered, maxCovered]is[1,10]. - The gaps within
[1,10]are[[5,6]]. Output:[[5,6]]
Notes and Edge Cases
- If the union of intervals is already contiguous (meaning there are no gaps), an empty list
[]should be returned. - Overlapping intervals should be merged and not create gaps.
- The input
intervalslist may be unsorted.
Constraints
- The number of intervals
len(intervals)will be between 1 and 2 * 10^5. - Interval endpoints will fit within a 32-bit signed integer.