Insert an interval into a sorted list of non-overlapping intervals and merge overlaps

Quick Overview

Insert a new interval into a sorted list of non-overlapping intervals, merge every interval it overlaps, and return the sorted result. It tests interval overlap rules, shared endpoints, empty input and a single linear pass over sorted data.

Insert an interval into a sorted list of non-overlapping intervals and merge overlaps

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a sorted array of non-overlapping intervals, where each interval is a pair `[start, end]` representing its start and end times. The array is sorted by start times in ascending order. You are also given a new interval `[start, end]` to insert into the array. Insert the new interval so that the resulting array remains sorted by start times and contains no overlapping intervals (merge overlapping intervals as needed). Return the updated array. You may build and return a new array. ### Function Signature ```python def insert_interval(intervals: list[list[int]], new_interval: list[int]) -> list[list[int]]: ``` ### Rules - Intervals are closed: `[a, b]` covers every point from `a` to `b`, including both ends. - Two intervals that share an endpoint overlap and must be merged: `[1,2]` and `[2,5]` become `[1,5]`. Intervals such as `[1,3]` and `[4,7]` share no point and stay separate. - Return the intervals as a list of `[start, end]` pairs sorted by start. ### Constraints - `0 <= len(intervals) <= 10^4` - `len(intervals[i]) == 2` - `0 <= start_i <= end_i <= 10^4` - `intervals` is sorted in ascending order by `start_i`, and no two of its intervals overlap. - `len(new_interval) == 2` - `0 <= start <= end <= 10^4` ### Examples ```text Input: intervals = [[2,4],[7,10]], new_interval = [3,6] Output: [[2,6],[7,10]] ``` The new interval `[3,6]` overlaps `[2,4]`, and together they cover `[2,6]`. The interval `[7,10]` starts after 6, so it is unchanged. ```text Input: intervals = [[1,3],[4,7],[9,12],[14,18]], new_interval = [5,10] Output: [[1,3],[4,12],[14,18]] ``` The new interval `[5,10]` overlaps both `[4,7]` and `[9,12]`, so the three merge into `[4,12]`. `[1,3]` ends before 5 and `[14,18]` starts after 12, so both stay as they are. ```text Input: intervals = [[1,2],[5,6]], new_interval = [2,5] Output: [[1,6]] ``` The new interval shares the point 2 with `[1,2]` and the point 5 with `[5,6]`, so all three merge into `[1,6]`.

Overview: Insert a new interval into a sorted list of non-overlapping intervals, merge every interval it overlaps, and return the sorted result. It tests interval overlap rules, shared endpoints, empty input and a single linear pass over sorted data.

Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given a sorted array of non-overlapping intervals, where each interval is a pair [start, end] representing its start and end times. The array is sorted by start times in ascending order. You are also given a new interval [start, end] to insert into the array.

Insert the new interval so that the resulting array remains sorted by start times and contains no overlapping intervals (merge overlapping intervals as needed). Return the updated array. You may build and return a new array.

Function Signature

def insert_interval(intervals: list[list[int]], new_interval: list[int]) -> list[list[int]]:

Rules

  • Intervals are closed: [a, b] covers every point from a to b , including both ends.
  • Two intervals that share an endpoint overlap and must be merged: [1,2] and [2,5] become [1,5] . Intervals such as [1,3] and [4,7] share no point and stay separate.
  • Return the intervals as a list of [start, end] pairs sorted by start.

Constraints

  • 0 <= len(intervals) <= 10^4
  • len(intervals[i]) == 2
  • 0 <= start_i <= end_i <= 10^4
  • intervals is sorted in ascending order by start_i , and no two of its intervals overlap.
  • len(new_interval) == 2
  • 0 <= start <= end <= 10^4

Examples

Input: intervals = [[2,4],[7,10]], new_interval = [3,6]
Output: [[2,6],[7,10]]

The new interval [3,6] overlaps [2,4], and together they cover [2,6]. The interval [7,10] starts after 6, so it is unchanged.

Input: intervals = [[1,3],[4,7],[9,12],[14,18]], new_interval = [5,10]
Output: [[1,3],[4,12],[14,18]]

The new interval [5,10] overlaps both [4,7] and [9,12], so the three merge into [4,12]. [1,3] ends before 5 and [14,18] starts after 12, so both stay as they are.

Input: intervals = [[1,2],[5,6]], new_interval = [2,5]
Output: [[1,6]]

The new interval shares the point 2 with [1,2] and the point 5 with [5,6], so all three merge into [1,6].