Interval List Intersections - Problem

You are given two lists of closed intervals, firstList and secondList, where firstList[i] = [start_i, end_i] and secondList[j] = [start_j, end_j]. Each list of intervals is pairwise disjoint and in sorted order.

Return the intersection of these two interval lists.

A closed interval [a, b] (with a <= b) denotes the set of real numbers x with a <= x <= b.

The intersection of two closed intervals is a set of real numbers that are either empty or represented as a closed interval. For example, the intersection of [1, 3] and [2, 4] is [2, 3].

Input & Output

Example 1 — Basic Intersection
$ Input: firstList = [[0,2],[5,10],[13,23],[24,25]], secondList = [[1,5],[8,12],[15,24],[25,26]]
Output: [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
💡 Note: Step by step: [0,2]∩[1,5]=[1,2], [5,10]∩[1,5]=[5,5], [5,10]∩[8,12]=[8,10], [13,23]∩[15,24]=[15,23], [24,25]∩[15,24]=[24,24], [24,25]∩[25,26]=[25,25]
Example 2 — No Intersections
$ Input: firstList = [[1,3],[5,9]], secondList = [[4,4],[10,12]]
Output: []
💡 Note: No overlap: [1,3] and [4,4] don't intersect, [1,3] and [10,12] don't intersect, [5,9] and [4,4] don't intersect, [5,9] and [10,12] don't intersect
Example 3 — Single Point Intersection
$ Input: firstList = [[1,7]], secondList = [[3,10]]
Output: [[3,7]]
💡 Note: Overlap exists: max(1,3)=3, min(7,10)=7, so intersection is [3,7]

Constraints

  • 0 ≤ firstList.length, secondList.length ≤ 1000
  • firstList.length + secondList.length ≥ 1
  • 0 ≤ starti < endi ≤ 109
  • endi < starti+1
  • 0 ≤ startj < endj ≤ 109
  • endj < startj+1

Visualization

Tap to expand
Interval List IntersectionsFirst List:[0,2][5,10][13,23]Second List:[1,5][8,12][15,24]Intersections:[1,2][5,5][8,10][15,23]Find overlapping regions where intervals from both lists intersect
Understanding the Visualization
1
Input
Two sorted lists of disjoint intervals
2
Process
Find overlapping regions between intervals
3
Output
List of intersection intervals
Key Takeaway
🎯 Key Insight: Use two pointers and advance whichever interval ends first to efficiently find all intersections in O(m+n) time
Asked in
Google 45 Facebook 38 Amazon 32 Microsoft 28
156.0K Views
Medium Frequency
~25 min Avg. Time
3.2K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen