Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

986. Interval List Intersections

Medium72.1% Acceptance
ArrayTwo Pointers
Asked by:
F
Facebook
Yandex
ProblemSolutions (6)VideosCompanies (8)Notes

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
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Y
V
Verkada
U
Uber
A
Amazon
+3

Approach

In #986 Interval List Intersections, you are given two lists of closed intervals that are individually sorted and non-overlapping. The goal is to find all intersections between intervals from the two lists. A key observation is that because both lists are already sorted, we can process them efficiently using the two pointers technique.

Start with one pointer for each list. For the current intervals, compute the potential intersection using start = max(start1, start2) and end = min(end1, end2). If start <= end, the intervals overlap and this range forms a valid intersection. After processing, move the pointer belonging to the interval that finishes first, since that interval cannot intersect with later intervals in the other list.

This strategy ensures each interval is processed only once, giving a linear scan across both lists. The approach runs in O(n + m) time with O(1) extra space (excluding the result list).

Complexity

ApproachTime ComplexitySpace Complexity
Two Pointers on Sorted Interval ListsO(n + m)O(1) auxiliary (excluding output)

Video Solution Available

NeetCode

View all video solutions

Solutions (6)

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.

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.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2#include <stdlib.h>
3
4int** intervalIntersection(int** A, int ASize, int* AColSize, 

Explanation

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.

Video Solutions

Watch expert explanations and walkthroughs

Merge Intervals - Sorting - Leetcode 56

NeetCode
10:15190,650 views

Asked By Companies

8 companies
F
Facebook
Y
Yandex
V
Verkada
U
Uber
A
Amazon
B
Bloomberg
G
Google
D
DoorDash

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Container With Most WaterMedium
3SumMedium
3Sum ClosestMedium
4SumMedium
More similar problems

Related Topics

ArrayTwo Pointers

Problem Stats

Acceptance Rate72.1%
DifficultyMedium
Companies8

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Interval List Intersections asked in FAANG interviews?

Yes, interval-based problems are common in technical interviews at companies like Google, Amazon, and Facebook. This problem tests understanding of interval overlap logic and efficient two-pointer traversal.

What is the optimal approach for Interval List Intersections?

The optimal approach uses the two pointers technique. Since both interval lists are sorted and non-overlapping internally, we can scan them simultaneously and compute overlaps using max start and min end. This results in linear time complexity.

What data structure is best for solving Interval List Intersections?

Arrays or lists of intervals are sufficient for this problem. The key technique is pointer traversal rather than a specialized data structure, as the intervals are already sorted.

Why does the two pointers technique work for Interval List Intersections?

Because both interval lists are sorted by start time, the earliest finishing interval determines which pointer should move next. This guarantees that all possible intersections are checked without revisiting intervals.

int
*
*
B
,
int
BSize
,
int
*
BColSize
,
int
*
returnSize
,
int
*
*
returnColumnSizes
)
{
5
int
a
=
0
,
b
=
0
;
6
int
capacity
=
1000
;
// Initial capacity based on constraints
7
int
*
*
ans
=
(
int
*
*
)
malloc
(
sizeof
(
int
*
)
*
capacity
)
;
8
*
returnColumnSizes
=
(
int
*
)
malloc
(
sizeof
(
int
)
*
capacity
)
;
9
*
returnSize
=
0
;
10
while
(
a
<
ASize
&&
b
<
BSize
)
{
11
int
start
=
fmax
(
A
[
a
]
[
0
]
,
B
[
b
]
[
0
]
)
;
12
int
end
=
fmin
(
A
[
a
]
[
1
]
,
B
[
b
]
[
1
]
)
;
13
if
(
start
<=
end
)
{
14
if
(
*
returnSize
==
capacity
)
{
15
capacity
*=
2
;
16
ans
=
(
int
*
*
)
realloc
(
ans
,
sizeof
(
int
*
)
*
capacity
)
;
17
*
returnColumnSizes
=
(
int
*
)
realloc
(
*
returnColumnSizes
,
sizeof
(
int
)
*
capacity
)
;
18
}
19
ans
[
*
returnSize
]
=
(
int
*
)
malloc
(
sizeof
(
int
)
*
2
)
;
20
ans
[
*
returnSize
]
[
0
]
=
start
;
21
ans
[
*
returnSize
]
[
1
]
=
end
;
22
(
*
returnColumnSizes
)
[
*
returnSize
]
=
2
;
23
(
*
returnSize
)
++
;
24
}
25
if
(
A
[
a
]
[
1
]
<
B
[
b
]
[
1
]
)
26
a
++
;
27
else
28
b
++
;
29
}
30
return
ans
;
31
}