Merge Overlapping Regions from Two Sorted Interval Lists
Greedy & IntervalsAuto-graded · 24 test casesLast updated
Tests two-pointer traversal on sorted interval lists to find all pairwise overlaps in linear time.
You are given two lists of closed intervals. Each list is sorted by starting position, and no two intervals within the same list overlap or touch each other. Your task is to find every region where an interval from the first list overlaps with an interval from the second list.
Two closed intervals overlap if they share at least one point. For example, [1, 3] and [3, 5] overlap at the single point 3, producing the intersection [3, 3].
Return all such overlapping regions as a list of intervals, sorted by starting position.
Constraints
- Each input list contains between 0 and 200,000 intervals
- Each interval is represented as
[start, end]wherestart <= end - All endpoint values are integers in the range
[-10^9, 10^9] - Within each input list, intervals are sorted and pairwise disjoint (no overlap or touching)
- Your solution must run in
O(m + n)time wheremandnare the lengths of the two lists
Examples
Example 1
Input: find_interval_overlaps([[0, 2], [5, 10], [13, 23], [24, 25]], [[1, 5], [8, 12], [15, 24], [25, 26]])
Output: [[1, 2], [5, 5], [8, 10], [15, 23], [24, 24], [25, 25]]
Example 2
Input: find_interval_overlaps([], [[1, 2]])
Output: []
Example 3
Input: find_interval_overlaps([[1, 2]], [])
Output: []
Example 4
Input: find_interval_overlaps([[0, 10]], [[1, 2], [4, 5], [8, 12]])
Output: [[1, 2], [4, 5], [8, 10]]
Hints
Hint 1
Use two pointers, one for each list, and examine pairs of intervals without backtracking.
Hint 2
When two intervals overlap, compute the intersection using max of starts and min of ends. Remember that closed intervals touching at a single point still count as overlapping.
Hint 3
After recording an intersection, advance the pointer pointing to whichever interval ends first—it cannot possibly intersect anything further in the other list.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem requires finding all pairwise intersections between intervals from two lists efficiently. The key insight is that because both lists are already sorted and contain non-overlapping intervals, we can use a two-pointer technique similar to merging two sorted arrays.
Algorithm:
- Initialize two pointers
iandj, one for each list, both starting at 0 - While both pointers are within their respective lists:
- Calculate the potential intersection of
first[i]andsecond[j] - The intersection starts at the maximum of the two start points
- The intersection ends at the minimum of the two end points
- If start ≤ end, the intervals overlap (using ≤ handles single-point overlaps)
- Advance the pointer pointing to the interval that ends first
- Calculate the potential intersection of
- Return all recorded intersections
Why advance the earlier-ending interval?
Whichever interval ends first cannot possibly intersect with any later interval in the other list. Since both lists are sorted and disjoint, all subsequent intervals in the other list must start after the current one ends. Therefore, we can safely move past it.
When both intervals end at the same point, advancing either pointer is safe. The reference implementation advances j.
Correctness:
The algorithm examines each pair of intervals at most once. Since one pointer advances on every iteration and we have at most m + n total advances, we examine at most m + n pairs. Each intersection is found exactly when the corresponding pair is examined, so no overlaps are missed or duplicated.
The output is automatically sorted because both input lists are sorted and we traverse them in order with forward-moving pointers.
Complexity
- Time complexity: O(m + n) where m and n are the lengths of the two input lists. Each iteration advances at least one pointer, giving at most m + n iterations total.
- Space complexity: O(1) auxiliary space beyond the output list. The output itself can contain at most m + n - 1 intervals.
Solution
from typing import List
def find_interval_overlaps(first: List[List[int]], second: List[List[int]]) -> List[List[int]]:
i = 0
j = 0
result = []
while i < len(first) and j < len(second):
start = max(first[i][0], second[j][0])
end = min(first[i][1], second[j][1])
if start <= end:
result.append([start, end])
if first[i][1] < second[j][1]:
i += 1
else:
j += 1
return result