Back to Practice
#0224

Merge Overlapping Intervals

MediumPython25 min20 XP

Problem

Given intervals [start, end], return merged intervals sorted by start. Treat touching intervals as mergeable.

Why This Matters

Interval merging appears in calendars, bookings, logs, timelines, and scheduling systems.

Function Signature

def merge_intervals(intervals):

Examples

Example 1
Input[[1, 3], [2, 6], [8, 10], [10, 12]]
Output[[1, 6], [8, 12]]

[1,3] overlaps [2,6], and [8,10] touches [10,12].

Constraints

  • Return the exact requested value.
  • Handle edge cases cleanly.
  • Prefer readable Python before micro-optimizing.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core example
Input[[1, 3], [2, 6], [8, 10], [10, 12]]
Expected[[1, 6], [8, 12]]

[1,3] overlaps [2,6], and [8,10] touches [10,12].

Hidden Test Categories
empty listalready separated intervalsnested intervals