An interval [start, end] represents a continuous range on some axis. In interviews, intervals model three kinds of domains:
Domain
Example
Typical problem
Time
Meetings, cron jobs, booking windows
Meeting Rooms, Calendar
Space
X-coordinates of balloons or segments
Burst Balloons, Skyline
Numeric
Ranges on the integer line
Data Stream, Range Module
Whatever the domain, the underlying algebra is the same — two numbers defining a range, and a question about how that range relates to others.
The four flavors of interval notation
Interval notation
Closed [a, b] → includes both endpointsOpen (a, b) → excludes both endpointsHalf-open [a, b) → includes a, excludes b ← most common in real systemsHalf-open (a, b] → excludes a, includes b
The universal overlap formula
Two intervals [a1, a2] and [b1, b2] overlap if and only if:
Greedy selection — max non-overlapping, min removals, min arrows
#435, #452, Activity Selection
Start ↑, then end ↓
key=(x[0], -x[1])
Remove Covered Intervals — process largest span first at each start
#1288
Event-based (sweep)
key=(x[0], x[1])
Counting active intervals at any point, peak usage, skyline
#253 alt, #732, #1094, #218
The 6 Common Approaches
Pattern recognition comes from knowing the toolkit. Nearly every interval problem you’ll see reduces to one of these six techniques.
#
Approach
When to reach for it
1
Sort + linear scan
Maintain a “last” interval, compare, merge or start fresh. Solves ~50% of all interval problems.
2
Sweep line (events)
Decompose into start/end events, sort, sweep, keep a running counter. Answers “how many overlap at point X?“
3
Greedy selection
Sort by end, greedily pick the earliest-finishing interval. For optimisation — max kept, min removals, min resources.
4
Priority queue (heap)
Track active intervals by end time in a min-heap. Answers “is any room free?” for resource allocation.
5
Two pointers
Two pre-sorted interval lists — advance the pointer whose interval ends first. Key for intersection problems.
6
Difference array
Bounded-integer coordinates — O(1) range updates via diff[l] += v; diff[r+1] -= v, then prefix sum to recover values.
Pattern recognition cheat sheet
Universal Edge Cases
Before writing a single line of code, run through this checklist:
#
Scenario
Usual handling
1
Empty input
Return [] / 0 / true — match the problem’s identity element
2
Single interval
Trivial base case — return as-is
3
All intervals identical
They all merge into one
4
No overlaps at all
Output equals input
5
One giant interval covering everything
Everything merges into it
6
Touching endpoints [1,2] & [2,3]
Overlap? Depends on closed vs. half-open — clarify
7
Zero-length [5,5]
Valid point-interval or degenerate — confirm
8
Negative coordinates
Make sure your math doesn’t assume positives
9
Very large values
Watch for start + end overflow on signed 32-bit
The 22 Pattern Map
Every interval problem in this season maps to one of six core techniques. Use this table as your index — the first 11 are the FAANG core, the next 11 are advanced extensions.
Core patterns (1–11)
#
Pattern
LC #
Sort by
Approach
1
Merge Intervals
#56
Start
Sort + linear scan
2
Insert Interval
#57
pre-sorted
Three-phase sweep
3
Meeting Rooms I
#252
Start
Consecutive pair check
4
Meeting Rooms II
#253
Start / events
Min-heap or sweep line
5
Non-Overlapping Intervals
#435
End
Greedy selection
6
Interval List Intersections
#986
pre-sorted
Two pointers
7
Employee Free Time
#759
Start
Merge + find gaps
8
Remove Interval
#1272
pre-sorted
Single pass + remnants
9
My Calendar I
#729
—
Overlap check per booking
10
My Calendar II
#731
—
Two-layer tracking
11
Data Stream as Disjoint Intervals
#352
maintained
Binary search + merge
Advanced patterns (12–22)
#
Pattern
LC #
Sort by
Approach
12
Sweep Line Template
—
Events
+1 / −1 decomposition
13
Min Arrows to Burst Balloons
#452
End
Greedy group counting
14
My Calendar III
#732
Events
Delta timeline / TreeMap
15
Car Pooling
#1094
Events
Sweep or difference array
16
Range Module
#715
maintained
Insert + Remove composed
17
Meeting Rooms III
#2402
Start
Two heaps (avail + busy)
18
Difference Array
#1109
—
O(1) range update + prefix sum
19
The Skyline Problem
#218
Events
Sweep + max-heap
20
Rectangle Area II
#850
Events
2-D sweep + merged y-coverage
21
Interval Tree Template
—
Median
Static BST of intervals
22
Task Scheduler
#621
Frequency
Closed-form math on counts
Comments
Comments are disabled in this environment. Set
PUBLIC_GISCUS_REPO, PUBLIC_GISCUS_REPO_ID, and
PUBLIC_GISCUS_CATEGORY_ID to enable.
Comments
Comments are disabled in this environment. Set
PUBLIC_GISCUS_REPO,PUBLIC_GISCUS_REPO_ID, andPUBLIC_GISCUS_CATEGORY_IDto enable.