Browse curriculum

Intervals

The Intervals Pattern

Intervals are sorted by one endpoint, then swept in order while tracking what is currently open. Almost every interval problem reduces to sorting correctly and then handling one comparison between the current interval and the previous one.

7

Interactive problems

2

Free to open

Recognise

How do you recognise a intervals problem?

The input is a list of start/end pairs — meetings, bookings, ranges — and the question involves merging, overlap, insertion or counting concurrency. If the input looks like [[1,3],[2,6],[8,10]], this is the pattern.

Complexity

What is the time complexity of the intervals pattern?

O(n log n), dominated by the sort; the sweep itself is O(n). Choosing the wrong sort key is the most common failure, and it is usually end time rather than start time.

Problems

Intervals practice problems

Each problem pairs a worked explanation with an interactive visualizer you step through yourself, plus the implementation in JavaScript, Python, Java and C++.

Complexity

Intervals problems by difficulty and complexity

The difficulty, running time and extra space of every intervals solution on this page, as each lesson derives them.

LessonDifficultyBestAverageWorstSpace
Intervals OverviewMediumΩ(n log n)Θ(n log n)O(n log n)O(n)
Merge IntervalsMediumΩ(n log n)Θ(n log n)O(n log n)O(n)
Insert IntervalMediumΩ(n)Θ(n)O(n)O(n)
Non-overlapping IntervalsMediumΩ(n log n)Θ(n log n)O(n log n)O(log n)
Meeting RoomsEasyΩ(n log n)Θ(n log n)O(n log n)O(log n)
Meeting Rooms IIMediumΩ(n log n)Θ(n log n)O(n log n)O(n)
Min Arrows to Burst BalloonsMediumΩ(n log n)Θ(n log n)O(n log n)O(log n)
Curriculum

Intervals is one of the 17 coding interview patterns, and its problems also appear in the DSA 75 interview sprint.

Directory

Every other coding interview pattern