Insert Interval - JS Solution

JS
Editorial

Solution explanation

Solution explanation

Approach

We are given a list of existing intervals and one new interval to insert. The goal is to return a new list of intervals that: includes the new interval, stays sorted by start time, and remains non-overlapping.

The key observation that makes this efficient is: the existing intervals are already sorted by start time and are non-overlapping. We take advantage of this structure and process the intervals from left to right in a single pass.

High-Level Strategy

  1. Step 1: Add all intervals that end before the new interval starts.
    Any interval whose end is strictly less than the new interval’s start cannot overlap with the new interval. Because the input is sorted and non-overlapping, we can safely copy all of these intervals directly into the result. Once we reach an interval that does not end before the new interval, we know there are no more "completely before" intervals left.
  2. Step 2: Merge all intervals that overlap with the new interval.
    Starting from the first interval that did not qualify for Step 1, we now check for overlap with the (possibly growing) new interval. An interval overlaps if its start is less than or equal to the new interval’s end.

    When we detect overlap, we merge by expanding the new interval to cover both ranges: the new start becomes min(currentStart, newStart), and the new end becomes max(currentEnd, newEnd).

    We repeat this as long as intervals continue to overlap. Because the input is sorted and non-overlapping, all overlapping intervals will appear contiguously. After this merging loop finishes, the "updated new interval" now represents one combined interval that covers everything that touched it. We then push this merged interval into the result.
  3. Step 3: Add all remaining intervals that come after the merged interval.
    At this point, we have already: (1) added all intervals that end before the new interval, and (2) merged every interval that overlaps with it. Any remaining intervals must start strictly after the merged interval ends. Since they cannot overlap with it, we can append them directly to the result.

Why This Works

  • Sorted + non-overlapping input: Because the original intervals never overlap and are sorted by start time, all intervals that are "before" the new interval come first, then any that "touch" it, then all that are "after" it. We never have to look backward.
  • Single pass: We walk through the intervals from left to right once, updating the new interval as needed. This gives us linear time behavior.
  • Output stays valid: We only ever push: intervals that are fully before, one merged interval that represents all overlaps, and intervals that are fully after. The final result is still sorted and still non-overlapping.

Complexity

Time Complexity: O(N), where N is the number of input intervals. We scan through the list once.

Space Complexity: O(N) for the output list of intervals. Aside from the result array, we only keep a few variables and reuse the new interval as we merge.

Time Complexity

O(N), where N is the number of input intervals. We scan through the list once.

Space Complexity

O(N), where N is the number of input intervals.
The resulting list can have at most N + 1 intervals — one more than the original list if the new interval does not merge with any existing ones.

Visucodize editorial solution titled JS Solution for LeetCode's Insert Interval coding problem. Includes code and may include explanation. You can animate the code step by step using the provided input by clicking the run button, or fork it locally to update the code and input for custom visualization. View the original problem at https://leetcode.com/problems/insert-interval.