All patterns
Interval Processing
Greedy Algorithms & Mathematical Optimization
Intermediate
Interactive
Sort ranges and merge every overlap into the active span.
Time
O(n log n)
Space
O(n) output
ActiveComparingMatch / bestIn windowFrontierVisitedExcludedAnswer
No frames to display.
The idea
Interval problems become linear after sorting by start. Keep one active merged range: overlapping intervals extend its end, while a gap commits it and begins a new range.
Analogy
This visualization makes the state transitions behind interval processing explicit, one decision at a time.
When to reach for it
- The problem exhibits this pattern's defining invariant.
- A direct brute-force approach repeats work or explores unnecessary choices.
- You need a standard interview-ready template.
Language-independent template
sort intervals by start
merged = []
for interval in intervals:
if gap: append interval
else: extend last intervalComplexity — time O(n log n), space O(n) output.
Common mistakes
- Choosing the wrong state or invariant.
- Updating state before preserving the value needed next.
- Missing a boundary or base case.
Practice progression
Canonical interview problems, ordered from foundation to advanced application.
Easy
Summary RangesLC 228
Compress consecutive sorted values into disjoint ranges.
Medium
Merge IntervalsLC 56
Merge every overlapping pair of intervals.
Hard
Employee Free TimeLC 759
Find finite intervals when every employee is free.
Variations
Change the input to exercise another path.
Adapt the invariant to a related optimization or counting problem.