Interval List Intersections
Find where two sorted lists of ranges overlap, walking both lists once with one pointer in each.
The problem
You get two lists of closed intervals, firstList and secondList. Inside each list the intervals are sorted and don't overlap each other. Return every range that is covered by an interval of the first list and an interval of the second list at the same time, in sorted order. Because the intervals are closed, two intervals that only touch, like [1, 3] and [3, 6], intersect in the single point [3, 3].
Input firstList = [[1, 4], [6, 9], [12, 13]], secondList = [[2, 7], [8, 15]]
Output [[2, 4], [6, 7], [8, 9], [12, 13]]
For example, [6, 9] overlaps both [2, 7] and [8, 15], which gives [6, 7] and [8, 9].
Input firstList = [[0, 3]], secondList = [[3, 5]]
Output [[3, 3]]
The two intervals share only the point 3, and a single point still counts.
Key constraint: Each list has up to 1,000 intervals, already sorted and disjoint, so you can walk both lists once instead of comparing every pair.
How to spot the pattern
- Two sorted lists that you combine as you walk them, like the merge step of merge sort.
- You need the overlap of ranges that come from two different sources.
The idea
Keep one pointer in each list. The two current intervals overlap exactly when the larger of their starts is at most the smaller of their ends, and that range is their intersection. Then move past the interval that ends first: it can't overlap anything further along in the other list, because those intervals start even later. The other interval stays, since it may still overlap the next one.
Brute force: Compare every interval in the first list with every interval in the second, which takes O(m * n) time.
Step by step
- 1Start
iat 0 infirstListandjat 0 insecondList. - 2Let
lobe the larger start andhithe smaller end offirstList[i]andsecondList[j]. - 3If
lo <= hi, add[lo, hi]to the answer. - 4Move forward the pointer whose interval ends first (when both end together, moving either one works).
- 5Stop when either list runs out and return the answer.
See the pattern move
The intervals pattern on a small example. Step through it, or press play.
Code
class Solution:
def intervalIntersection(self, firstList: List[List[int]], secondList: List[List[int]]) -> List[List[int]]:
answer = []
i = j = 0
while i < len(firstList) and j < len(secondList):
lo = max(firstList[i][0], secondList[j][0])
hi = min(firstList[i][1], secondList[j][1])
if lo <= hi:
answer.append([lo, hi])
# The interval that ends first can't overlap anything else.
if firstList[i][1] < secondList[j][1]:
i += 1
else:
j += 1
return answerComplexity
Time
O(m + n)
Space
O(m + n)
Every step moves one pointer forward, so there are at most m + n steps; the answer has at most m + n intervals.
Watch out for
- Moving both pointers after finding an intersection: the interval that ends later may still overlap the next interval in the other list.
- Using
lo < hi, which drops single-point intersections like [3, 3]. - Moving the pointer whose interval starts first instead of the one that ends first.
Follow-up questions
How would you get the union of the two lists instead?
Merge the two sorted lists by start, like the merge step of merge sort, and run the Merge Intervals sweep over that combined order.
What if the lists were not sorted?
Sort each list by start first; if intervals inside one list could overlap, merge them with Merge Intervals before running the two-pointer walk. That adds O(m log m + n log n).
In Hinglish
Dono lists mein ek-ek pointer rakho. Overlap ho toh [bada start, chhota end] answer mein daalo, phir jiska interval pehle khatam hota hai use aage badhao.