DSA Tracker

Medium

Insert Interval

A medium Intervals problem included in Love Babbar 450, Striver A2Z. Below: the roles whose interviews prioritise this topic, and how to practise it.

Topic
Intervals
Sheets
2
Core for
6 roles
Platform
LeetCode

The problem

Given a sorted list of non-overlapping intervals and a new interval, insert the new interval into the list, merging any overlapping intervals.

Example 1

Input
intervals = [[1,3],[6,9]], newInterval = [2,5]
Output
[[1,5],[6,9]]
Why
The new interval [2,5] overlaps with [1,3], so they merge into [1,5].

Example 2

Input
intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
Output
[[1,2],[3,10],[12,16]]
Why
The new interval [4,8] overlaps with [3,5], [6,7], and [8,10], merging them into [3,10].

Constraints

  • 0 <= intervals.length <= 10^4
  • intervals[i].length == 2
  • 0 <= intervals[i][0] <= intervals[i][1] <= 10^5
  • newInterval.length == 2

How to think about it

Updated 2026-09-09

The existing intervals already sit in sorted order with zero collisions between them. That means any incoming interval can only slice through a single contiguous block of entries. Everything strictly to the left copies over untouched, the overlapping cluster collapses into one bounding box, and everything to the right trails behind without a change.

Approaches, worst first

  1. Append, sort, and merge

    time O(n log n) · space O(n)

    Push the new interval onto the list, re-sort every segment by start coordinate, and run the generic interval merging sweep. It works cleanly, but throws away the fact that the source array was already sorted and pairwise disjoint.

  2. Three-phase single passWrite this one

    time O(n) · space O(n)

    Walk left to right: collect all intervals ending before the new one starts, absorb every interval that overlaps by taking the minimum start and maximum end into the runner, push the absorbed interval, and append the remaining suffix.

Where people lose marks · 3
  • An empty input array is valid under constraints; the output must contain just the new interval rather than failing indexing.
  • Using strict inequality `end < newStart` to detect non-overlap on the left, but forgetting that equal boundaries such as [1, 2] and [2, 3] touch and must merge when intervals are closed.
  • Inserting the merged interval at the end of the loop instead of immediately when the first strictly-later interval is encountered, causing suffix entries to get misordered.

The theory behind it

Intervals — the ground this problem stands on. All Intervals problems

What Intervals is

An interval is a continuous range of numbers defined by two values: a start point and an end point. Think of a calendar event booked from two to four o'clock, or a cut segment on a ruler. Because each interval covers every number between its boundaries, two intervals can sit apart with empty room between them, touch at an edge, or overlap across a shared span of numbers.

When to reach for it

Reach for intervals when the input consists of start and end pairs representing time slots, schedules, ranges, or geometric segments. Phrasings asking to merge overlapping blocks, insert a new meeting into a busy calendar, find the minimum number of conference rooms needed, or count how many intervals must be removed to eliminate overlaps all point directly to interval patterns. Whenever problems involve resource contention over time, think intervals.

How the pattern works

The opening move is almost always sorting the intervals by their start times, or occasionally by their end times. Once ordered, compare the current interval with the previous one. If the new start time is less than or equal to the previous end time, the two intervals collide; merge them by stretching the previous end time to the maximum of both ends. If they do not collide, the previous interval is finished, so append it to the result and start tracking the new one. For room counts, split intervals into separate start and end events.

What each operation costs

OperationTime
sort intervals by start or end timeO(n log n)
merge sorted intervals in a single passO(n)
track active meetings using a min-heapO(n log n)
What usually goes wrong with Intervals
  • Merging two overlapping intervals by taking the second interval end without using the maximum of both ends, which shrinks an interval when the first one completely swallowed the second.
  • Treating intervals that touch at the exact same boundary point as disjoint when the problem statement defines boundaries as closed and inclusive.
  • Forgetting to append the final merged interval to the output list after the iteration loop finishes.

Which roles need this problem

Intervals is a core topic for these 6 roles — if you're targeting one of them, this problem is early in your path, not optional.

Secondary for 11 more roles, including Frontend Engineer, Full-Stack Developer, SDET / QA Engineer.

Track this in your role's order

Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.

Start free

More Intervals problems

Problem set and role mapping as of .