Insert Interval
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.
A sorted, non-overlapping list intervals, and one interval newInterval = [start, end].
The list after inserting and merging, sorted by start.
[2,5] overlaps [1,3], so they become [1,5]. [6,9] starts after 5, so it stays.
[4,8] overlaps [3,5], [6,7] and [8,10] (it touches 8). Together they cover [3,10].
- โข 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
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Add the new interval, sort everything, merge like Merge Intervals
Copy the left side, swallow the overlaps into the new interval, copy the right side
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:
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.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Intervals translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.