22. Insert Interval
mediumAsked at InstacartInsert a new interval into a sorted, non-overlapping list and merge as needed — Instacart uses this when a new delivery window is injected into an existing shopper schedule without disturbing confirmed slots.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
You are given an array of non-overlapping intervals sorted in ascending order by start time, and a single newInterval to insert. Insert newInterval into intervals and merge if necessary. Return the resulting array of intervals, also sorted by start time with no overlapping intervals.
Constraints
0 <= intervals.length <= 10^4intervals[i].length == 20 <= start_i <= end_i <= 10^5intervals is sorted by start_i in ascending ordernewInterval.length == 20 <= newInterval[0] <= newInterval[1] <= 10^5
Examples
Example 1
intervals = [[1,3],[6,9]], newInterval = [2,5][[1,5],[6,9]]Explanation: [2,5] overlaps [1,3], so they merge into [1,5].
Example 2
intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8][[1,2],[3,10],[12,16]]Approaches
1. Insert then merge all
Append the new interval, sort by start, then run a standard merge-intervals pass.
- Time
- O(n log n)
- Space
- O(n)
function insert(intervals, newInterval) {
intervals.push(newInterval);
intervals.sort((a, b) => a[0] - b[0]);
const result = [intervals[0]];
for (let i = 1; i < intervals.length; i++) {
const last = result[result.length - 1];
if (intervals[i][0] <= last[1]) {
last[1] = Math.max(last[1], intervals[i][1]);
} else {
result.push(intervals[i]);
}
}
return result;
}Tradeoff:
2. Linear single pass
Since intervals is already sorted, walk through in three phases: copy all intervals that end before newInterval starts, merge all overlapping, copy all that start after newInterval ends.
- Time
- O(n)
- Space
- O(n)
function insert(intervals, newInterval) {
const result = [];
let i = 0;
const n = intervals.length;
// Phase 1: intervals completely before newInterval
while (i < n && intervals[i][1] < newInterval[0]) {
result.push(intervals[i++]);
}
// Phase 2: merge overlapping
while (i < n && intervals[i][0] <= newInterval[1]) {
newInterval[0] = Math.min(newInterval[0], intervals[i][0]);
newInterval[1] = Math.max(newInterval[1], intervals[i][1]);
i++;
}
result.push(newInterval);
// Phase 3: intervals completely after newInterval
while (i < n) result.push(intervals[i++]);
return result;
}Tradeoff:
Instacart-specific tips
Instacart schedules shoppers in pre-sorted time blocks. The three-phase linear pass is the expected answer — it exploits the sorted invariant and runs in O(n) versus O(n log n) for re-sorting. Interviewers specifically look for whether you handle the edge cases: new interval fully before all existing ones, fully after, or spanning all of them.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
Practice these live with InterviewChamp.AI
Drill Insert Interval and other Instacart interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →