Insert Interval

Difficulty: Medium

You're given a list of intervals sorted by start time, where no two overlap or touch. Insert one new interval in the right position.

If it overlaps any existing intervals, merge them all into one, so the list stays sorted with no overlaps.

Examples

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

[2,5] overlaps [1,3], so they merge into [1,5]. [6,9] is untouched.

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

[4,8] overlaps [3,5], [6,7], and [8,10], so all four intervals merge into [3,10].

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

With no existing intervals, the new one is simply inserted on its own.

Constraints

  • 0 <= intervals.length <= 10^4

  • intervals is sorted by start time and has no overlaps

  • 0 <= start <= end <= 10^5

Approach

The quick way: add the new interval, sort by start time, and merge overlapping neighbors, as in Merge Intervals. It works, but it ignores that the input was already sorted.

Since the list is sorted and non-overlapping, one left-to-right pass is enough:

  • Copy every interval that ends before the new one starts.
  • Absorb every overlapping interval by widening the new interval to cover it.
  • Copy everything left.

Solutions

Brute Force — Sort and Merge

Treat it like Merge Intervals: add the new interval, sort by start time, then walk through once, merging each interval into the last result whenever they overlap.

function insert(intervals, newInterval) {
  const merged = [...intervals, newInterval];
  merged.sort((a, b) => a[0] - b[0]);

  const result = [];
  for (const interval of merged) {
    const last = result[result.length - 1];
    if (!last || last[1] < interval[0]) {
      result.push(interval);
    } else {
      last[1] = Math.max(last[1], interval[1]);
    }
  }
  return result;
}

Time: O(n log n) — dominated by the sort · Space: O(n) for the merged array and result

Optimal — Single Pass (Before / Overlap / After)

The input is already sorted and non-overlapping, so skip sorting. Walk through once:

  • Copy every interval that ends before the new interval starts. It can't overlap.
  • Absorb every interval whose start isn't past the new interval's current end, widening the new interval's start/end to cover it.
  • Push the merged interval.
  • Copy everything left. None of it can overlap.
function insert(intervals, newInterval) {
  const result = [];
  const n = intervals.length;
  let i = 0;
  let [start, end] = newInterval;

  while (i < n && intervals[i][1] < start) {
    result.push(intervals[i]);
    i++;
  }

  while (i < n && intervals[i][0] <= end) {
    start = Math.min(start, intervals[i][0]);
    end = Math.max(end, intervals[i][1]);
    i++;
  }
  result.push([start, end]);

  while (i < n) {
    result.push(intervals[i]);
    i++;
  }

  return result;
}

Time: O(n) — one linear pass, no sorting needed · Space: O(n) for the result