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

56. Merge Intervals

Medium48.3% Acceptance
ArraySorting
Asked by:
F
Facebook
Amazon
ProblemSolutions (12)VideosCompanies (25)Notes

Problem Statement

Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.

Example 1:

Input: intervals = [[1,3],[2,6],[8,10],[15,18]]
Output: [[1,6],[8,10],[15,18]]
Explanation: Since intervals [1,3] and [2,6] overlap, merge them into [1,6].

Example 2:

Input: intervals = [[1,4],[4,5]]
Output: [[1,5]]
Explanation: Intervals [1,4] and [4,5] are considered overlapping.

Constraints:

  • 1 <= intervals.length <= 104
  • intervals[i].length == 2
  • 0 <= starti <= endi <= 104
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
A
G
Google
B
Bloomberg
S
Salesforce
+20

Approach

The key idea in Merge Intervals is to combine overlapping intervals into a single continuous range. A common strategy is to first sort the intervals by their starting time. Sorting ensures that any overlapping intervals will appear next to each other, making them easier to merge.

After sorting, iterate through the intervals and maintain a result list. Compare the current interval with the last interval stored in the result. If the intervals overlap (i.e., the current start is less than or equal to the previous end), merge them by updating the end value using max(end1, end2). Otherwise, simply append the current interval to the result.

This greedy approach works because once intervals are sorted, each interval only needs to be compared with the last merged interval. The overall time complexity is dominated by the sorting step, while the merging pass is linear.

Complexity

ApproachTime ComplexitySpace Complexity
Sort intervals and merge overlapping rangesO(n log n)O(n)

Video Solution Available

take U forward

View all video solutions

Solutions (12)

Sort and Merge Intervals

The key to solving this problem is to first sort the intervals based on the starting time. Once sorted, we can iterate over the intervals and merge them if they overlap. Two intervals overlap if the start of the current interval is less than or equal to the end of the previous interval.

Time Complexity: O(n log n), due to sorting.
Space Complexity: O(n), required for the output array.

CC++JavaPythonC#JavaScript
1#include <stdlib.h>
2#include <string.h>
3
4int compare(const void *a, const void *b) {
5    return (*(int **)a)[0] - (*(int **)b)[0];
6}
7
8int** merge(int** intervals, int intervalsSize, int* intervalsColSize, int* returnSize, int** returnColumnSizes) {
9    if (intervalsSize == 0) {
10        *returnSize = 0;
11        return NULL;
12    }
13    qsort(intervals, intervalsSize, sizeof(int*), compare);
14    int** result = (int**)malloc(intervalsSize * sizeof(int*));
15    *returnColumnSizes = (int*)malloc(intervalsSize * sizeof(int));
16    int index = 0;
17    for (int i = 0; i < intervalsSize; ++i) {
18        if (index == 0 || result[index - 1][1] < intervals[i][0]) {
19            result[index] = (int*)malloc(2 * sizeof(int));
20            result[index][0] = intervals[i][0];
21            result[index][1] = intervals[i][1];
22            (*returnColumnSizes)[index] = 2;
23            index++;
24        } else {
25            result[index - 1][1] = result[index - 1][1] > intervals[i][1] ? result[index - 1][1] : intervals[i][1];
26        }
27    }
28    *returnSize = index;
29    return result;
30}

Explanation

In C, sorting is performed using qsort, with a custom comparator function. We iterate through each interval and merge overlapping intervals into a single one, storing the results in a new 2D array.

Interval Insertion Method

Another approach involves consecutive comparisons and inserting intervals into a new list if they do not overlap with the current interval being processed.

Time Complexity: O(n log n).
Space Complexity: O(n).

CC++JavaPythonC#JavaScript
1#include 

Video Solutions

Watch expert explanations and walkthroughs

Merge Intervals | Leetcode | Problem-6 | Brute-Optimal | C++/Java

take U forward
11:00262,085 views

Asked By Companies

25 companies
F
Facebook
A
Amazon
G
Google
B
Bloomberg
S
Salesforce
A
Apple
M
Microsoft
U
Uber
A
Adobe
I
IBM
L
LinkedIn
S
Snapchat
W
Walmart Global Tech
V
VMware
O
Oracle
Y
Yandex
S
Shopee
T
Twitter
C
Cisco
I
Intuit
N
Nvidia
M
Morgan Stanley
B
Bytedance
T
TikTok
G
GEICO

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

3SumMedium
3Sum ClosestMedium
4SumMedium
Group AnagramsMedium
More similar problems

Related Topics

ArraySorting

Problem Stats

Acceptance Rate48.3%
DifficultyMedium
Companies25

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Merge Intervals asked in FAANG interviews?

Yes, Merge Intervals is a classic interval problem frequently asked in technical interviews at companies like Google, Amazon, and Facebook. It tests understanding of sorting, greedy strategies, and interval manipulation.

Why do we sort intervals before merging them?

Sorting ensures that intervals are processed in increasing order of their start times. This guarantees that any overlapping intervals appear next to each other, allowing us to merge them with a simple linear scan.

What is the optimal approach for Merge Intervals?

The optimal approach is to first sort the intervals based on their start time and then iterate through them while merging overlapping intervals. By maintaining a result list and updating the end time when overlaps occur, the problem can be solved efficiently in a single pass after sorting.

What data structure is commonly used in Merge Intervals problems?

Arrays or lists are typically used to store intervals and the merged results. Since we process intervals sequentially after sorting, a dynamic list structure works well for appending or updating merged ranges.

Previous Problem

Jump Game

Next Problem

Insert Interval

<stdlib.h>
2
#
include
<string.h>
3
4
int
compare
(
const
void
*
a
,
const
void
*
b
)
{
5
return
(
*
(
int
*
*
)
a
)
[
0
]
-
(
*
(
int
*
*
)
b
)
[
0
]
;
6
}
7
8
int
*
*
merge
(
int
*
*
intervals
,
int
intervalsSize
,
int
*
intervalsColSize
,
int
*
returnSize
,
int
*
*
returnColumnSizes
)
{
9
if
(
intervalsSize
==
0
)
{
10
*
returnSize
=
0
;
11
return
NULL
;
12
}
13
qsort
(
intervals
,
intervalsSize
,
sizeof
(
int
*
)
,
compare
)
;
14
int
*
*
result
=
(
int
*
*
)
malloc
(
intervalsSize
*
sizeof
(
int
*
)
)
;
15
*
returnColumnSizes
=
(
int
*
)
malloc
(
intervalsSize
*
sizeof
(
int
)
)
;
16
int
index
=
0
;
17
for
(
int
i
=
0
;
i
<
intervalsSize
;
++
i
)
{
18
if
(
index
==
0
||
result
[
index
-
1
]
[
1
]
<
intervals
[
i
]
[
0
]
)
{
19
result
[
index
]
=
(
int
*
)
malloc
(
2
*
sizeof
(
int
)
)
;
20
result
[
index
]
[
0
]
=
intervals
[
i
]
[
0
]
;
21
result
[
index
]
[
1
]
=
intervals
[
i
]
[
1
]
;
22
(
*
returnColumnSizes
)
[
index
]
=
2
;
23
index
++
;
24
}
else
{
25
if
(
result
[
index
-
1
]
[
1
]
<
intervals
[
i
]
[
1
]
)
{
26
result
[
index
-
1
]
[
1
]
=
intervals
[
i
]
[
1
]
;
27
}
28
}
29
}
30
*
returnSize
=
index
;
31
return
result
;
32
}

Explanation

This method involves sorting first and directly inserting into the result list, verifying overlap in consecutive intervals.