Skip to content
AI360Xpert
Beta
Difficulty: MediumIntervals

Insert Interval

Problem in Plain English

You are given a sorted list of non-overlapping intervals plus one new interval to insert. Splice it into the right position, merging every interval it overlaps, and return the still-sorted non-overlapping result.

Problem Statement

Given a sorted list of non-overlapping closed intervals intervals and one closed interval newInterval, insert the newcomer and merge every overlap. Return intervals sorted by start with no overlaps. Closed means both endpoints belong to the interval: sharing even one endpoint counts as overlap.

The input list may be empty and an interval may be a single point. The implementations below preserve both arguments and return newly owned interval rows, so changing the result later does not change either input.

Constraints

  • 0≤n=intervals.length≤1040 \leq n = \mathrm{intervals.length} \leq 10^4.
  • Every existing row and newInterval contain exactly two endpoints.
  • 0≤start≤end≤1050 \leq \mathrm{start} \leq \mathrm{end} \leq 10^5 for every interval, including the newcomer.
  • Existing intervals are sorted by start and do not overlap; each existing end is strictly smaller than the following start.

Examples

Example 1

Input
intervals = [[1,3],[6,9]], newInterval = [2,5]
Output
[[1,5],[6,9]]

The newcomer overlaps [1,3], producing [1,5]. The strict gap between 5 and 6 keeps [6,9] separate.

Example 2

Input
intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
Output
[[1,2],[3,10],[12,16]]

The newcomer joins [3,5] and [6,7], then touches [8,10] at 8. Closed endpoints make that contact an overlap, yielding [3,10].

Example 3

Input
intervals = [], newInterval = [5,5]
Output
[[5,5]]

With no existing blocks, the point interval is the entire answer. Its endpoints may be equal, and the returned row is a copy.

Example 4

Input
intervals = [[1,3],[6,9]], newInterval = [3,6]
Output
[[1,9]]

The newcomer touches the first block at 3 and the second at 6, connecting both into one closed block.

Example 5

Input
intervals = [[1,9]], newInterval = [3,5]
Output
[[1,9]]

The newcomer is already contained in the existing interval, so the covered region stays [1,9].

Intuition

Sorted, non-overlapping input splits around the newcomer into three regions: intervals entirely before it, intervals absorbed into it, and intervals entirely after it. In Example 1, [1,3] overlaps [2,5], widening the pending block to [1,5]; [6,9] then lies wholly after that block. There is no need to reorder the input or compare every pair. Because endpoints are included, [1,3] and [3,5] must merge; a strict gap is required to keep two blocks apart.

Approaches

Append Sort Merge

Solution Details

Reveal Append Sort Merge: intuition, complexity, and code

Hints

Hint 1
Every existing interval is either fully left of the new one, fully right of it, or overlapping it.
Hint 2
Overlapping intervals never need to be emitted immediately: fold them into the new interval bounds instead.
Hint 3
Once an interval starts strictly after the folded new interval ends, emit the new interval first and copy the rest untouched.

Edge Cases

  • An empty intervals list returns just [newInterval].
  • A newInterval touching an endpoint, such as [3,5] against [1,3], still overlaps and merges into [1,5].
  • A newInterval spanning everything collapses the whole list into a single interval.

Common Mistakes and Interview Tips

  • Treating touching closed endpoints as a gap: [1,3] and [3,5] share point 3 and must merge.
  • Forgetting the final pending append loses the newcomer when it belongs after every input row or when input is empty.
  • Appending a borrowed input row or setting pending directly to an input row makes later result mutation change an argument. Copy every emitted or carried row.
  • Sorting the original outer list mutates its order; the sorting baseline sorts a separate outer copy.

Key Takeaway

Sorted input turns insertion into one linear pass: everything left is copied, everything overlapping is folded, and everything right is copied after the folded interval is emitted.