Insert Interval — Coding Interview Walkthrough
You’re in a coding interview. The interviewer says: “You have a list of non-overlapping intervals sorted by start time. Insert a new interval and merge any overlaps.” You know this is a linear scan — no sort needed because the input is already sorted. The challenge is the boundary conditions: when does the new interval overlap, when does the current interval go before it, and when does it go after?
The Problem
You are given an array of non-overlapping intervals intervals sorted in ascending order by start time, and a new interval newInterval. Insert newInterval into intervals, merging any overlapping intervals.
Example
Input: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
Output: [[1,2],[3,10],[12,16]]
Already comfortable with the solution? Practice it in a mock interview →
What Interviewers Are Actually Testing
| Skill | What Interviewers Observe |
|---|---|
| Case analysis | Do you identify the three cases cleanly? |
| Boundary conditions | Do you use <= vs. < correctly for overlap? |
| No re-sort | Do you recognize no sort step is needed? |
| Edge cases | Empty intervals, newInterval before/after all? |
Step 1: The Three Cases
For each existing interval relative to newInterval:
- Ends before (
curr[1] < new_start): no overlap, add as-is. - Overlaps (
curr[0] <= new_end): merge by expanding bounds. - Starts after (
curr[0] > new_end): insert merged newInterval, then add rest.
Step 2: The Key Insight
Treat the insertion as a running interval that expands as it absorbs overlapping neighbors. Scan left to right — anything before gets added directly, anything overlapping merges, everything after the overlap triggers the insertion.
Python Implementation
def insert(intervals: list[list[int]], newInterval: list[int]) -> list[list[int]]:
result = []
i = 0
n = len(intervals)
# Add intervals that end before newInterval starts
while i < n and intervals[i][1] < newInterval[0]:
result.append(intervals[i])
i += 1
# Merge overlapping intervals
while i < n and intervals[i][0] <= newInterval[1]:
newInterval[0] = min(intervals[i][0], newInterval[0])
newInterval[1] = max(intervals[i][1], newInterval[1])
i += 1
result.append(newInterval)
# Add remaining intervals
result.extend(intervals[i:])
return result
Time & Space Complexity
| Aspect | Complexity |
|---|---|
| Time | O(n) — single scan |
| Space | O(n) — output list |
Common Interview Mistakes
- Wrong overlap condition. Use
<=not<— adjacent intervals like[1,3]and[3,5]should merge. - Forgetting to append merged interval. Many candidates exit the merge loop and skip appending
newInterval. - Unnecessary sort. Input is already sorted — sorting costs O(n log n) for no reason.
What a Strong Candidate Sounds Like
“The input is already sorted, so I scan left to right in O(n). Three cases: intervals ending before the new one get added as-is, overlapping ones merge by expanding bounds, and once past the overlap I insert the merged interval and append the rest.”
Example Interview Dialogue
Interviewer: What’s the overlap condition?
Candidate: Two intervals [a,b] and [c,d] overlap if c <= b — the second starts no later than the first ends. I use intervals[i][0] <= newInterval[1] in the merge loop.
Related Problems to Practice
- Merge Intervals — Same merging logic on unsorted input.
- Meeting Rooms — Interval overlap detection.
Practice This in a Mock Interview
Start a mock interview for Insert Interval on Intervu.
Further reading:
- How to Prepare for a Coding Interview in 2026, the complete roadmap
- Why LeetCode alone isn’t enough, and what to practice instead
- Practice any LeetCode problem as a live mock interview
- The Grind 75 Pathway, a structured study plan with AI practice links
- Browse all walkthroughs on GitHub