2Brute2Best
๐Ÿ”ฅ0

Insert Interval

MediumยทLeetCode #57 Pro OnlyBasic Reinforcement
LeetCode
01

Problem Statement

You get a list of intervals [start, end] that is already sorted by start, and no two of them overlap. You also get one more interval, newInterval.

Put newInterval into the list. If it overlaps some intervals, join them all into one. Return the new list, still sorted and still with no overlaps.

Intervals that only touch (like [1,5] and [5,7]) count as overlapping.

Input

A sorted, non-overlapping list intervals, and one interval newInterval = [start, end].

Output

The list after inserting and merging, sorted by start.

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

[2,5] overlaps [1,3], so they become [1,5]. [6,9] starts after 5, so it stays.

Example 2
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] (it touches 8). Together they cover [3,10].

Constraints
  • โ€ข 0 โ‰ค intervals.length โ‰ค 10^4
  • โ€ข intervals[i].length == 2, and 0 โ‰ค start โ‰ค end โ‰ค 10^5
  • โ€ข intervals is sorted by start and has no overlaps
  • โ€ข newInterval.length == 2, and 0 โ‰ค start โ‰ค end โ‰ค 10^5
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

โ€œalready sorted by startโ€
โ†’ The sort step of Merge Intervals is already done for us โ€” don't pay for it again.
โ€œno two intervals overlapโ€
โ†’ Only the new interval can cause a merge, so overlaps form one run around it.
โ€œinsert and merge if neededโ€
โ†’ It is the Merge Intervals step, but with only one interval that can grow.
03

Approach Complexity Ladder

Brute Force โ€” append and run Merge IntervalsO(N log N)O(N)

Add the new interval, sort everything, merge like Merge Intervals

Optimal โ€” three phases in one passO(N)O(N)

Copy the left side, swallow the overlaps into the new interval, copy the right side

Brute Force Intuition

Push newInterval onto a copy of the list, sort by start, then merge exactly as in Merge Intervals: compare each interval with the last merged one and stretch its end.

Recognition rule for this sub-pattern:

Sort by start. If next.start โ‰ค current.end, they overlap โ€” extend current.end. Otherwise start a new interval.
Remember this ยท Intervals
Trigger
Input is [start, end] pairs; overlap, merge, insert, meeting rooms, free time, minimum removals.
Think
After sorting, do I only ever need to compare against the most recent interval?
Key question
Sort by start or by end โ€” which one makes the greedy choice obvious?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Intervals translate into production architectures and low-level memory mechanics.