Insert Interval
Reported by candidates from Apple's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The detail that matters in this Apple OA, reported in July 2026, is that the intervals are already sorted and already non-overlapping. That's a gift, and the question is whether you spot it. Insert Interval looks like a merge-intervals problem, so candidates reach for sorting and burn time on an O(n log n) solution they don't need. It's an array problem with a clean single pass. If you blank mid-assessment, StealthCoder sits invisibly on your screen and gives you the pass structure in real time. Better to walk in already knowing the three-phase shape, though.
The problem
You are given a list of non-overlapping closed intervals intervals, sorted by start time, and one closed interval newInterval. Insert newInterval so that the result remains sorted and contains no overlapping intervals. Merge every interval that overlaps the inserted interval, and return the resulting list. Function insertInterval(intervals: int[][], newInterval: int[]) → int[][] Examples Example 1 intervals = [[1,3],[6,9]] newInterval = [2,5] return = [[1,5],[6,9]] The inserted interval 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] return = [[1,2],[3,10],[12,16]] The new interval overlaps [3,5], [6,7], and [8,10], producing [3,10]. Constraints 0 <= intervals.length <= 10^4 Each interval has exactly two integers [start, end] with start <= end. intervals is sorted by start time and contains no overlapping intervals. newInterval has exactly two integers with newInterval[0] <= newInterval[1]. Every endpoint is in [-10^9, 10^9].
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is three phases in one linear scan. First, copy every interval whose end is less than newInterval's start. Second, while the current interval's start is at most newInterval's end, merge by taking min of starts and max of ends. Push the merged result. Third, copy everything left. That's O(n) time and no sort. The common pitfall is the overlap condition. Closed intervals mean touching endpoints like [1,3] and [3,5] do overlap, so use less-than for the first phase and less-than-or-equal for the second. Also handle the empty list, where you just return [newInterval]. And don't mutate while iterating in a way that skips elements. If your head goes blank on the boundaries live, StealthCoder is the hedge, since it reads the problem and hands you the loop. Test with Example 2 by hand first.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Insert Interval cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as insert interval. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Apple's OA.
Apple reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Insert Interval FAQ
How hard is Insert Interval really?+
Medium on paper, easy once you see the pattern. The input is sorted and non-overlapping, so a single pass works. Most failures come from off-by-one overlap checks, not from the algorithm. If you can write three small loops cleanly, you're done.
What's the trick for the Apple version?+
Split the scan into three phases: intervals fully before the new one, intervals that overlap it, and intervals fully after. Merge during the middle phase using min start and max end. No sorting is needed because the input is already sorted.
Do touching intervals like [1,3] and [3,5] merge?+
Yes. The problem says closed intervals, so sharing an endpoint counts as overlap. Keep the first phase strict, where interval end is less than new start, and let the second phase use start less than or equal to new end.
What edge cases should I test?+
Empty intervals list, new interval entirely before or after everything, new interval swallowing all intervals, and a new interval that touches an endpoint. Also negative values, since endpoints go down to -10^9. Run Example 2 by hand to check the merge.
How do I prepare in 48 hours?+
Write this solution from scratch twice without looking. Then do Merge Intervals and Non-overlapping Intervals to lock in the boundary logic. Focus on the overlap condition and the final append of the merged interval, which is where most bugs hide.