986. Interval List Intersections
Asked at Meta
Difficulty
Medium
Time
O(m + n)
Space
O(1)
Pattern
Array
Companies
1
Problem
You get two lists of closed intervals, firstList and secondList. Each interval is written as [start, end] and covers every number from start to end, including both ends. Inside each list the intervals do not overlap each other, and they are sorted by start.
Return a list of all the intervals that belong to both lists — that is, every range of numbers covered by some interval in firstList and also by some interval in secondList. The answer must be sorted by start, and the returned intervals must not overlap each other.
Two intervals [a1, a2] and [b1, b2] overlap when the later start is not bigger than the earlier end. Their shared part is [max(a1, b1), min(a2, b2)]. If that range is empty, the two intervals do not intersect. Note that a single point counts: [1, 5] and [5, 8] share [5, 5].
Where do the two lists overlap?
Constraints
- ●
0 <= firstList.length, secondList.length <= 1000 - ●
firstList.length + secondList.length >= 1 - ●
0 <= starti < endi <= 10^9 - ●
0 <= startj < endj <= 10^9 - ●
Each list is sorted by start and has no overlapping intervals inside it
Examples
firstList = [[0,2],[5,10],[13,23],[24,25]], secondList = [[1,5],[8,12],[15,24],[25,26]][[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]firstList = [[1,3],[5,9]], secondList = [][]Asked At
1 companies| Company | Difficulty | |
|---|---|---|
| Meta | Medium | View all Meta questions → |
How to Think About It
Brute force: For each interval in list A, compare against every interval in list B for overlap.
Two pointers: Walk through both lists simultaneously, advancing the pointer of the interval with the smaller end.
Intersection of two intervals [a1, a2] and [b1, b2] is [max(a1,b1), min(a2,b2)] if max start <= min end.
Advance the pointer whose interval ends first since it cannot intersect with any later interval in the other list.
Two pointers approach achieves O(m + n) time and O(1) extra space.
Optimal Approach
Use two pointers i and j on the two interval lists. For each pair, compute the intersection as [max(start_i, start_j), min(end_i, end_j)]. If this is valid (start <= end), add it to the result. Then advance whichever pointer corresponds to the interval with the smaller end value, because that interval cannot overlap with any future interval in the other list.
Two pointers, advance the one that ends first
What Trips People Up in Real Interviews
Confirm both input lists are already sorted by start time.
Clarify that intersections are inclusive: [1,3] and [3,5] intersect at [3,3].
Explain why we advance the pointer with the smaller end value.
Edge cases: no intersections at all, one list empty, intervals touching at a single point.
Discuss that the output size is bounded by m + n.
Solution Code
O(m + n) timedef intervalIntersection(firstList, secondList):
result = []
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:
result.append([lo, hi])
if firstList[i][1] < secondList[j][1]:
i += 1
else:
j += 1
return resultFrequently Asked Questions
What is the Interval List Intersections problem?
Given two lists of closed intervals sorted by start, find all their intersections. Each interval is [start, end] with start <= end. Return the list of intersection intervals.
How do you solve Interval List Intersections?
The optimal approach runs in O(m + n) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Interval List Intersections?
Interval List Intersections is asked at Meta. It is a medium difficulty problem.
What are common mistakes on Interval List Intersections?
- Confirm both input lists are already sorted by start time.
- Clarify that intersections are inclusive: [1,3] and [3,5] intersect at [3,3].
- Explain why we advance the pointer with the smaller end value.
- Edge cases: no intersections at all, one list empty, intervals touching at a single point.
- Discuss that the output size is bounded by m + n.