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 Intersections INPUT firstList (Blue) 0-2 5-10 13-23 24-25 secondList (Red) 1-5 8-12 15-24 25-26 Input Arrays: firstList = [[0,2],[5,10],[13,23],[24,25]] secondList = [[1,5],[8,12],[15,24],[25,26]] Two Pointers: i, j i j start at 0 ALGORITHM STEPS 1 Initialize Pointers i=0, j=0, result=[] 2 Find Intersection start = max(A[i][0], B[j][0]) end = min(A[i][1], B[j][1]) if start <= end: result.add([start, end]) // Valid intersection 3 Move Pointer Advance pointer with smaller end if A[i][1] < B[j][1]: i++ else: j++ 4 Repeat Until Done while i < len(A) and j < len(B) Time: O(m+n) | Space: O(1) FINAL RESULT Intersections Found (Purple) 1-2 5 8-10 15-23 24 25 Output: [[1,2],[5,5],[8,10], [15,23],[24,24],[25,25]] Execution Trace: [0,2] ^ [1,5] --> [1,2] [5,10] ^ [1,5] --> [5,5] [5,10] ^ [8,12] --> [8,10] [13,23] ^ [8,12] --> none [13,23] ^ [15,24] --> [15,23] [24,25] ^ [15,24] --> [24,24] [24,25] ^ [25,26] --> [25,25] // Done: i reaches end 6 Intersections OK Key Insight: The intersection of [a,b] and [c,d] is [max(a,c), min(b,d)] only if max(a,c) <= min(b,d). We advance the pointer pointing to the interval that ends first, since it cannot intersect with any future intervals from the other list. This ensures O(m+n) time complexity. TutorialsPoint - Interval List Intersections | Two Pointer Optimal Approach
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