Interview's MateDSA Visualizer
Interview's Mate

DSA Visualizer

Learn by watching algorithms run

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
All topics
PROBLEM-SOLVING TECHNIQUES

Merge Intervals

Sort by start, then merge or count overlaps in one pass.

Intermediate7 problems3 interactive subtopicsO(n log n) time · O(n) space
Practice in AI DSA
WalkthroughOverviewRecognize itMistakes & edge casesKnowledge check (3)Practice
Algorithms & custom examples (3)Open in full playground →
STEP 1/4Sort by start
sorted by start
INPUT, SORTED BY START
[1, 3]
[2, 6]
[8, 10]
[15, 18]
MERGED
0369121518
  • active this step
  • visited / done
Space Play←→ StepR Replay
1 / 4
PYTHONintervals = [[1, 3], [8, 10], [2, 6], [15, 18]]
1def merge(intervals):
▶2 intervals.sort(key=lambda x: x[0])
3 merged = []
4 for start, end in intervals:
5 if merged and start <= merged[-1][1]:
6 merged[-1][1] = max(merged[-1][1], end)
7 else:
8 merged.append([start, end])
9 return merged
STEP 1 OF 4Setup

Sort by start

The order becomes [1, 3], [2, 6], [8, 10], [15, 18].

VARIABLES

current
-
merged
[]
02

Intuition & complexity

The core mental model, trade-offs, and Big-O cost.

What it is

Sort intervals by start. Walk through them, extending the current interval while the next one overlaps and starting a new one when it doesn’t.

Why it matters

Once sorted, an interval can only overlap the one before it, so a single pass is enough.

Think of it like this

Merging calendar bookings: once they’re in time order, you only compare each with the previous one.

Use it when

  • Merging overlapping ranges
  • Inserting a new interval
  • Counting rooms or resources needed at the same time

Avoid it when

  • Intervals arrive live and must be queried as they come - use a sorted structure
  • The data is points, not ranges

Complexity cheatsheet

OperationTimeSpace
Sort, then mergeO(n log n)O(n)
Compare all pairsO(n²)O(n)
Rooms needed (sort starts and ends)O(n log n)O(n)

Often confused with: greedy interval scheduling - it sorts by end time to pick the most non-overlapping intervals.

03

How to recognize it

Clues in interview prompts that point straight to this pattern.

intervalsoverlapmeeting roomsmergeschedule
  • Sorting is O(n log n) and usually the dominant cost
  • Ask whether touching ends like [1, 2] and [2, 3] count as overlapping
04

Mistakes & edge cases

Catch common off-by-one bugs and corner cases before submitting.

Common mistakes

  • Forgetting to sort first
  • Setting the end to the new end instead of the max - [1, 10] then [2, 3] would shrink to [1, 3]
  • Changing the caller’s list when it must stay intact

Edge cases to test

  • An empty list
  • One interval inside another
  • Touching intervals like [1, 2] and [2, 3]
Interview tip: Clarify whether intervals are closed or half-open before choosing < or <=.
05

Knowledge check

Quick check to lock in the intuition.

Question 1 of 3

Merging [1, 10] with the next interval [2, 3], what should the end be?

06

Practice

Apply what you just watched on real interview problems.

Generate Array problem in AI DSA Practice →
  • BeginnerSummary RangesLeetCode 228 ↗ (opens in a new tab)
  • IntermediateMerge IntervalsLeetCode 56 ↗ (opens in a new tab)
  • AdvancedData Stream as Disjoint IntervalsLeetCode 352 ↗ (opens in a new tab)
Binary Search on AnswerMonotonic Stack