Skip to content
AI360Xpert
Beta
Difficulty: MediumIntervals

Merge Intervals

Problem in Plain English

You are given an unsorted list of intervals and must combine every group of overlapping ones. Return the merged non-overlapping intervals covering exactly the same points.

Problem Statement

Given an array intervals where intervals[i] = [start_i, end_i], merge all overlapping intervals.

Two intervals overlap when they share at least one point, so touching intervals like [1,4] and [4,5] merge into [1,5]. Return the non-overlapping intervals covering exactly the same ranges as the input.

Constraints

  • 1 <= intervals.length <= 10^4
  • intervals[i].length == 2
  • 0 <= start_i <= end_i <= 10^4

Examples

Example 1

Input
intervals = [[1,3],[2,6],[8,10],[15,18]]
Output
[[1,6],[8,10],[15,18]]

Intervals [1,3] and [2,6] overlap and merge into [1,6]; the other two touch nothing.

Example 2

Input
intervals = [[1,4],[4,5]]
Output
[[1,5]]

The intervals touch at 4, which counts as overlapping, so they merge into [1,5].

Example 3

Input
intervals = [[5,5]]
Output
[[5,5]]

A zero-length closed interval still covers point 5. One interval needs no merge and is returned as an independently owned row.

Example 4

Input
intervals = [[8,10],[1,3],[2,9]]
Output
[[1,10]]

The last interval [2,9] bridges the earlier disjoint blocks [1,3] and [8,10]. Absorbing just one block would leave overlapping output and fail to represent the fully merged result.

Example 5

Input
intervals = [[1,10],[2,3],[10,10]]
Output
[[1,10]]

The nested interval contributes no new endpoints, and the point interval at 10 touches the outer interval. Taking the maximum end prevents the nested interval from shrinking the covered range.

Intuition

Each merged block represents a connected covered range, so combining two overlapping intervals takes the smaller start and larger end. Touching endpoints count as overlap here. Example 1 combines [1,3] with [2,6] to form [1,6]. A new interval can bridge several existing blocks, so the unsorted approach must inspect every block; start-time sorting makes only the most recent merged block relevant.

Approaches

Pairwise Merge

Solution Details

Reveal Pairwise Merge: intuition, complexity, and code

Hints

Hint 1
Overlapping intervals are only guaranteed to sit together after sorting.
Hint 2
Once sorted by start, each interval can only overlap the most recently merged block.
Hint 3
Extend that block end to the larger of the two ends, or append a fresh block when the new start clears it.

Edge Cases

  • A single interval returns unchanged with no merging needed.
  • Touching intervals [1,4] and [4,5] merge into [1,5].
  • A fully nested interval like [2,3] inside [1,10] disappears into the outer bounds.
  • Equal-start intervals merge into the longest end; duplicate rows contribute no new points.
  • A zero-length interval [5,5] is legal and merges with any closed interval containing point 5.
  • An unsorted bridge such as [2,9] after [8,10] and [1,3] must absorb both blocks.

Common Mistakes and Interview Tips

  • An unsorted approach must compare against every maintained block. Merely comparing the last block misses distant overlaps and bridge intervals.
  • Setting the merged end to the current interval end instead of the maximum, which shrinks blocks that fully contain the newcomer.
  • Using a strict overlap comparison fails to merge touching closed endpoints; this differs from Meeting Rooms.
  • Sorting the caller container or extending an input row accidentally mutates the supplied intervals; copy the outer container and allocate owned result rows.
  • Stopping pairwise merging after one absorbed block misses a newcomer that bridges several blocks.
  • Interview tip: state the disjoint-block invariant and explain why only the final block can extend after sorting.

Key Takeaway

Sorting by start lets finalized blocks stay closed: compare each incoming interval only with the final merged block, extending its maximum end or appending an owned new block.