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

57. Insert Interval

Medium42.5% Acceptance
Array
Asked by:
G
Google
L
LinkedIn
ProblemHints (3)Solutions (12)VideosCompanies (5)Notes

Problem Statement

You are given an array of non-overlapping intervals intervals where intervals[i] = [starti, endi] represent the start and the end of the ith interval and intervals is sorted in ascending order by starti. You are also given an interval newInterval = [start, end] that represents the start and end of another interval.

Insert newInterval into intervals such that intervals is still sorted in ascending order by starti and intervals still does not have any overlapping intervals (merge overlapping intervals if necessary).

Return intervals after the insertion.

Note that you don't need to modify intervals in-place. You can make a new array and return it.

Example 1:

Input: intervals = [[1,3],[6,9]], newInterval = [2,5]
Output: [[1,5],[6,9]]

Example 2:

Input: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
Output: [[1,2],[3,10],[12,16]]
Explanation: Because the new interval [4,8] overlaps with [3,5],[6,7],[8,10].

Constraints:

  • 0 <= intervals.length <= 104
  • intervals[i].length == 2
  • 0 <= starti <= endi <= 105
  • intervals is sorted by starti in ascending order.
  • newInterval.length == 2
  • 0 <= start <= end <= 105
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
F
Facebook
A
Amazon
R
Robinhood

Approach

The key idea in Insert Interval is to maintain the sorted, non-overlapping structure of the existing intervals while correctly placing the new interval. Because the intervals are already sorted by start time, you can process them in a single linear pass.

First, add all intervals that end before the new interval starts since they cannot overlap. Next, handle the overlapping section by merging intervals. During this phase, update the start and end of the new interval using min(start) and max(end) across overlaps. Finally, once merging is complete, insert the merged interval and append the remaining intervals that start after it.

This approach works efficiently because each interval is processed only once. The overall time complexity is O(n), where n is the number of intervals, and the extra space is mainly for storing the resulting list of intervals.

Complexity

ApproachTime ComplexitySpace Complexity
Linear Scan with Interval MergingO(n)O(n)

Video Solution Available

NeetCode

View all video solutions

Problem Hints

Use these hints if you're stuck. Try solving on your own first.

1
Hint 1

Intervals Array is sorted. Can you use Binary Search to find the correct position to insert the new Interval.?

2
Hint 2

Can you try merging the overlapping intervals while inserting the new interval?

3
Hint 3

This can be done by comparing the end of the last interval with the start of the new interval and vice versa.

Ready to see the solutions?View Solutions

Solutions (12)

Approach 1: Insert and Merge Intervals

In this approach, we'll first identify where the new interval should be inserted to maintain order. After insertion, we'll iterate over the array and merge overlapping intervals.

Time Complexity: O(n), where n is the number of intervals.
Space Complexity: O(n) due to the allocation for the result.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2#include <stdlib.h>
3
4struct Interval {
5    int start;
6    int end;
7};
8
9struct Interval* insert(struct Interval* intervals, int intervalsSize, struct Interval newInterval, int* returnSize) {
10    struct Interval* result = malloc(sizeof(struct Interval) * (intervalsSize + 1));
11    int i, j = 0;
12    for (i = 0; i < intervalsSize && intervals[i].end < newInterval.start; ++i) {
13        result[j++] = intervals[i];
14    }
15    for (; i < intervalsSize && intervals[i].start <= newInterval.end; ++i) {
16        newInterval.start = fmin(newInterval.start, intervals[i].start);
17        newInterval.end = fmax(newInterval.end, intervals[i].end);
18    }
19    result[j++] = newInterval;
20    while (i < intervalsSize) {
21        result[j++] = intervals[i++];
22    }
23    *returnSize = j;
24    return result;
25}

Explanation

The C solution allocates memory for a new set of intervals that can hold the original intervals plus the new one. It adds all the intervals that end before the new interval's start. Next, it merges overlapping intervals with the new interval. Finally, all remaining intervals are appended after the merged intervals. The function returns the new list of intervals.

Approach 2: Binary Search for Insert Position then Merge

This approach optimizes finding the insert position using binary search. After inserting the new interval, it merges overlapping intervals. This is slightly more efficient when the intervals list is large.

Time Complexity: O(n), even with binary search (O(log n) for insertion, O(n) for merging).
Space Complexity: O(n), to store the new list of intervals.

CC++JavaPythonC#JavaScript
1#

Video Solutions

Watch expert explanations and walkthroughs

Insert Interval - Leetcode 57 - Python

NeetCode
11:51186,576 views

Asked By Companies

5 companies
G
Google
L
LinkedIn
F
Facebook
A
Amazon
R
Robinhood

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

Two SumEasy
Median of Two Sorted ArraysHard
Container With Most WaterMedium
3SumMedium
More similar problems

Related Topics

Array

Problem Stats

Acceptance Rate42.5%
DifficultyMedium
Companies5

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Insert Interval asked in FAANG interviews?

Yes, Insert Interval is a common interview problem at companies like Google, Amazon, and Meta. It tests your understanding of interval merging, array traversal, and edge case handling in algorithm design.

What is the optimal approach for Insert Interval?

The optimal approach is a linear scan through the sorted intervals. You first add non-overlapping intervals before the new interval, then merge overlapping intervals, and finally append the remaining intervals. This ensures the list stays sorted and non-overlapping in O(n) time.

What data structure is best for solving Insert Interval?

A dynamic array or list is typically sufficient for this problem. Since intervals are already sorted, you can iterate once and build a result list while merging overlaps, making arrays an efficient and simple choice.

What is the difference between Insert Interval and Merge Intervals?

Merge Intervals requires merging overlaps in an entire list of intervals, usually after sorting. Insert Interval assumes the list is already sorted and non-overlapping, and the task is to insert a new interval while preserving that property.

Previous Problem

Merge Intervals

Next Problem

Spiral Matrix II

include
<stdio.h>
2
#
include
<stdlib.h>
3
4
int
binarySearch
(
int
*
*
intervals
,
int
size
,
int
target
)
{
5
int
low
=
0
,
high
=
size
-
1
;
6
while
(
low
<=
high
)
{
7
int
mid
=
low
+
(
high
-
low
)
/
2
;
8
if
(
intervals
[
mid
]
[
0
]
==
target
)
9
return
mid
;
10
else
if
(
intervals
[
mid
]
[
0
]
<
target
)
11
low
=
mid
+
1
;
12
else
13
high
=
mid
-
1
;
14
}
15
return
low
;
16
}
17
18
int
*
*
insert
(
int
*
*
intervals
,
int
intervalsSize
,
int
*
newInterval
,
int
newIntervalSize
,
int
*
returnSize
)
{
19
int
pos
=
binarySearch
(
intervals
,
intervalsSize
,
newInterval
[
0
]
)
;
20
int
*
*
result
=
(
int
*
*
)
malloc
(
sizeof
(
int
*
)
*
(
intervalsSize
+
1
)
)
;
21
int
k
=
0
;
22
for
(
int
i
=
0
;
i
<
pos
;
i
++
)
{
23
result
[
k
]
=
(
int
*
)
malloc
(
sizeof
(
int
)
*
2
)
;
24
result
[
k
]
[
0
]
=
intervals
[
i
]
[
0
]
;
25
result
[
k
++
]
[
1
]
=
intervals
[
i
]
[
1
]
;
26
}
27
int
*
mergedInterval
=
malloc
(
sizeof
(
int
)
*
2
)
;
28
mergedInterval
[
0
]
=
newInterval
[
0
]
;
29
mergedInterval
[
1
]
=
newInterval
[
1
]
;
30
while
(
pos
<
intervalsSize
&&
intervals
[
pos
]
[
0
]
<=
mergedInterval
[
1
]
)
{
31
mergedInterval
[
0
]
=
fmin
(
mergedInterval
[
0
]
,
intervals
[
pos
]
[
0
]
)
;
32
mergedInterval
[
1
]
=
fmax
(
mergedInterval
[
1
]
,
intervals
[
pos
]
[
1
]
)
;
33
pos
++
;
34
}
35
result
[
k
++
]
=
mergedInterval
;
36
while
(
pos
<
intervalsSize
)
{
37
result
[
k
]
=
(
int
*
)
malloc
(
sizeof
(
int
)
*
2
)
;
38
result
[
k
]
[
0
]
=
intervals
[
pos
]
[
0
]
;
39
result
[
k
++
]
[
1
]
=
intervals
[
pos
]
[
1
]
;
40
pos
++
;
41
}
42
*
returnSize
=
k
;
43
return
result
;
44
}

Explanation

This C solution integrates binary search to locate the accurate insertion index for the new interval before merging. Although binary search optimizes finding the position, the merge process remains linear.