Skip to main content

Interval List Intersections - Solution & Explanation

MediumArrayTwo Pointers11 min readAsked at: Amazon, Microsoft, Apple +9
Practice this problem

Problem Statement

You are given two lists of closed intervals, firstList and secondList, where firstList[i] = [starti, endi] and secondList[j] = [startj, endj]. 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].

 

Example 1:

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]]

Example 2:

Input: firstList = [[1,3],[5,9]], secondList = []
Output: []

 

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

Approach Overview

Problem Overview: You get two lists of closed intervals. Each list is already sorted and internally non-overlapping. The task is to return all intersections between intervals from the first list and the second list.

The challenge is efficiently identifying overlapping regions between two ordered interval sequences. Since both lists are sorted by start time, you can exploit that structure instead of comparing every pair.

Approach 1: Brute Force Comparison (O(n * m) time, O(1) space)

The straightforward solution compares every interval in the first list with every interval in the second list. For each pair [a1, a2] and [b1, b2], compute the overlap using start = max(a1, b1) and end = min(a2, b2). If start <= end, an intersection exists and should be added to the result.

This approach works because interval intersection can be determined in constant time. However, you still check all possible pairs using nested loops. With n intervals in the first list and m in the second, the time complexity becomes O(n * m). Space complexity remains O(1) excluding the output. This approach is easy to implement but inefficient for large inputs.

Approach 2: Two Pointers Technique (O(n + m) time, O(1) space)

A better solution uses the two pointers pattern to scan both interval lists simultaneously. Start with pointer i at the beginning of the first list and pointer j at the beginning of the second. At each step, compare intervals A[i] and B[j].

Compute the intersection exactly the same way as the brute force method: start = max(A[i][0], B[j][0]) and end = min(A[i][1], B[j][1]). If start <= end, add the interval to the result. After checking overlap, move the pointer that has the smaller end value. This works because the interval that ends first cannot intersect with any future intervals in the other list.

Each interval is processed at most once. That reduces the runtime to O(n + m), where n and m are the sizes of the two lists. The algorithm uses only a few variables, so the extra space complexity is O(1). This technique is common in ordered data problems involving arrays and interval merging.

Recommended for interviews: Interviewers typically expect the Two Pointers solution. Starting with the brute force approach shows you understand how interval overlap works. Then optimizing to the O(n + m) two-pointer scan demonstrates algorithmic thinking and awareness of sorted input properties.

Approach 1: Two Pointers Technique

In this approach, we use a two-pointer technique to traverse both lists of intervals simultaneously. We check for intersections between the intervals pointed by our two pointers. If there's an intersection, we record it. We then progress the pointer that points to the interval with the smaller endpoint. This ensures that we consider new overlapping possibilities as the intervals in each list are disjoint and sorted.

We use pointers a and b to iterate over firstList and secondList respectively. We find the maximum start and minimum end for the current intervals pointed by these pointers to determine overlap. If there's an overlap, it is added to the result list. We move the pointer with the smaller endpoint to ensure progress.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n + m), where n and m are the number of intervals in the two lists. This is because we process each interval at most once.

Space Complexity: O(1), apart from the output space, since only a few variables are used.

Try this approach in the editor →

Approach 2: Default Approach

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Two Pointers Technique

Time Complexity: O(n + m), where n and m are the number of intervals in the two lists. This is because we process each interval at most once.

Space Complexity: O(1), apart from the output space, since only a few variables are used.

Default Approach—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Pair ComparisonO(n * m)O(1)Small inputs or when interval lists are not guaranteed to be sorted
Two Pointers TechniqueO(n + m)O(1)Best choice when both interval lists are sorted and non-overlapping internally

Video Solution

Interval List Intersections | Leetcode #986 • Techdose • 29,154 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Interval List Intersections easy or hard?
Interval List Intersections is considered a medium-level problem. The main challenge is recognizing that sorted intervals allow a linear two-pointer scan instead of brute force comparisons.
How to solve Interval List Intersections in O(n)?
Use a two-pointer scan across both sorted interval lists. For intervals A[i] and B[j], compute the overlap using max(start) and min(end). If they intersect, record the result. Move the pointer that has the smaller ending value. This ensures each interval is processed once, giving O(n + m) runtime.
What is the best approach for Interval List Intersections?
The optimal approach uses the two pointers technique. Since both interval lists are sorted, you can scan them simultaneously and compare one interval from each list at a time. By advancing the pointer of the interval that ends first, the algorithm processes each interval once, achieving O(n + m) time complexity and O(1) extra space.
Is Interval List Intersections asked at Google/Amazon/Meta?
Interval-based problems appear frequently in interviews at companies like Google, Amazon, and Meta. Variations involving interval merging, overlap detection, and scheduling are common because they test two-pointer reasoning and edge-case handling.
What data structure is used in Interval List Intersections?
The problem mainly uses arrays (or lists) of intervals. The algorithm relies on pointer indices to traverse both arrays and compute overlaps using simple arithmetic comparisons between interval boundaries.
What is the time complexity of Interval List Intersections?
The optimal two-pointer solution runs in O(n + m) time, where n and m are the lengths of the two interval lists. Each pointer moves forward at most once per interval. The brute force method compares every pair and runs in O(n * m) time.
Interval List Intersections Python or Java solution approach?
Both Python and Java implementations follow the same logic. Maintain two indices for the interval lists, compute overlap with max(start) and min(end), append valid intersections, then advance the pointer with the smaller end value. The overall complexity remains O(n + m).

Ready to solve this problem?

Practice Interval List Intersections with our built-in code editor and test cases.

Practice on FleetCode