↓Skip to main content
  1. LeetCode/

LeetCode 986: Interval List Intersections

·2 mins· ·
LeetCode Medium Two-Pointers Sweep-Line Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-02-01

  • Submissions: Accepted (first try)

Approach
#

Both lists are sorted and internally disjoint, so use two pointers as when merging two sorted arrays.

  • The intersection is [max(start1, start2), min(end1, end2)], non-empty when lo <= hi (closed intervals, so touching endpoints count).
  • The interval that ends first cannot intersect any later interval in the other list (those start later), so advance its pointer.

My Solution
#

class Solution:
    def intervalIntersection(self, firstList: List[List[int]], secondList: List[List[int]]) -> List[List[int]]:
        i = j = 0
        res = []
        while i < len(firstList) and j < len(secondList):
            start1, end1 = firstList[i]
            start2, end2 = secondList[j]
            lo, hi = max(start1, start2), min(end1, end2)
            if lo <= hi:
                res.append([lo, hi])
            if end1 < end2:
                i += 1                             # firstList[i] ends first
            else:
                j += 1
        return res

Complexity
#

Time O(n + m), space O(1) excluding the output.

Mistakes to Avoid
#

  • Checking lo < hi: the closed interval [5, 5] is a valid intersection.
  • Advancing the pointer whose interval starts first: advance the one that ends first.

Interview Explanation
#

Both lists are sorted, so two pointers each hold one interval. The intersection runs from the larger start to the smaller end and is added when the start does not exceed the end. The interval that ends first cannot meet any later interval, so its pointer moves. Time O(n + m).

Related

LeetCode 3555: Smallest Subarray to Sort in Every Sliding Window
·2 mins
LeetCode Medium Two-Pointers Greedy Sorting Array Monotonic-Stack
LeetCode 3555 note: every length-k window is one instance of LeetCode 581; with n only 1000, two O(k) scans per window are enough
LeetCode 581: Shortest Unsorted Continuous Subarray
·2 mins
LeetCode Medium Two-Pointers Greedy Sorting Array Monotonic-Stack
LeetCode 581 note: scanning left to right with a running max, the last index below it is the right boundary; scanning right to left with a running min, the last index above it is the left boundary
LeetCode 1574: Shortest Subarray to be Removed to Make Array Sorted
·2 mins
LeetCode Medium Two-Pointers Binary-Search Array Monotonic-Stack
LeetCode 1574 note: what remains is a non-decreasing prefix plus a non-decreasing suffix; find the longest of each, then join them with two pointers
LeetCode 1877: Minimize Maximum Pair Sum in Array
·1 min
LeetCode Medium Two-Pointers Greedy Sorting Array
LeetCode 1877 note: after sorting, pair the smallest with the largest, the second smallest with the second largest, and take the largest pair sum with converging pointers
LeetCode 2105: Watering Plants II
·2 mins
LeetCode Medium Two-Pointers Simulation Array
LeetCode 2105 note: Alice from the left and Bob from the right, simulated with converging pointers; when they meet at the same plant, whoever has more water waters it
LeetCode 809: Expressive Words
·3 mins
LeetCode Medium Two-Pointers String Array
LeetCode 809 note: two pointers compare runs of equal characters; when the lengths differ, the run in s must be at least 3 and no shorter than the word’s run, and both strings must end together