You start with a list of non-overlapping intervals that is already ordered by start value. A single new interval arrives, and you must slot it into the list so that the final collection stays sorted and free of overlaps, merging with any existing intervals that it touches.
Return the updated list. Because the original data is presorted, a full re-sort is unnecessary; a single linear pass can place and merge the newcomer.
Example 1
Input: intervals = [[1,3],[6,9]], newInterval = [2,5]
Output: [[1,5],[6,9]]
The new range 2-5 overlaps 1-3, so they fuse into 1-5 while 6-9 stays untouched.
Example 2
Input: intervals = [[1,2],[3,5],[6,7],[8,10]], newInterval = [4,8]
Output: [[1,2],[3,10]]
4-8 spans 3-5, 6-7 and 8-10, collapsing all three into 3-10.
Constraints
0 ≤ intervals.length ≤ 10^4intervals[i].length == 2 and newInterval.length == 20 ≤ start ≤ end ≤ 10^5intervals is sorted by start in ascending order with no overlapsSee the step-by-step animation, the intuition, and clean code in every language — free, no credit card.
FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.
750+ engineers trained · frontend, backend, AWS & AI