Coding interview problems โบ Intervals
๐ Intervals Medium
Insert Interval
Insert Interval explained: add a new range to a sorted list of non-overlapping intervals in one O(n) pass with three phases, tested Python and a live visual.
Also searched as: insert into sorted intervals ยท insert and merge interval ยท add interval to non-overlapping list
Try it live Free
Merge Intervals & Meeting Rooms
On the Merge Intervals timeline, drag a new interval across existing ones and watch the overlapping ones fuse into a single busy block.
Also useful: Binary Search
The problem
You are given intervals that are sorted by start and do not overlap, plus one new interval. Insert the new interval and merge wherever needed so the result is still sorted and non-overlapping.
The key insight
Because the list is already sorted, it splits into three runs: intervals that end before the new one starts (copy them), intervals that overlap it (absorb them by widening the new interval), and intervals that start after it ends (copy them). No re-sorting is needed.
Step-by-step approach
- Walk with index
i. Whileintervals[i]ends beforenewstarts, copy it to the output. - While
intervals[i]starts at or beforenewends, widennewto cover it (min of starts, max of ends). - Append the widened
new. - Copy all remaining intervals.
- Return the output.
Python solution
def insert(intervals, new_interval):
out, i, n = [], 0, len(intervals)
s, e = new_interval
while i < n and intervals[i][1] < s: # entirely before
out.append(intervals[i]); i += 1
while i < n and intervals[i][0] <= e: # overlapping: absorb
s = min(s, intervals[i][0])
e = max(e, intervals[i][1])
i += 1
out.append([s, e])
out.extend(intervals[i:]) # entirely after
return outComplexity
Each interval is visited once across the three loops. The output list holds up to n + 1 intervals.
Common mistakes
- Re-sorting the list, which wastes the fact that it is already sorted.
- Using strict comparisons in the wrong place so that touching intervals like
[1, 2]and[2, 3]are not merged. - Forgetting the cases where the new interval goes at the very beginning, the very end, or into an empty list.
Frequently asked questions
What is the time and space complexity of Insert Interval?
O(n) time with one pass, and O(n) space for the new list.
Can binary search make Insert Interval faster?
Binary search can find where the overlapping run begins and ends in O(log n), but building the new list still costs O(n), so the overall bound does not change unless you use a mutable tree structure.
What if the input intervals are not sorted?
Then append the new interval and run the general Merge Intervals algorithm, which sorts first in O(n log n).
Preparing for placements?
Take the 2-minute readiness check to find your weak patterns, then follow the placement path from DSA core to system design.
All coding interview problems More Intervals Merge Intervals & Meeting Rooms lab Placement prep guides
Problem statements on this site are written in our own words. Problem names are the common names used across interview-prep communities; BitBootcamp is an independent learning platform and is not affiliated with LeetCode or any company that asks these questions.