Editor

Merge Overlapping Intervals

Advanced · Write the code · from 50 Python Practice Problems with Instant Answer Checking (Beginner to Advanced)

From the lesson: 50 Python Practice Problems with Instant Answer Checking (Beginner to Advanced)

Fifty problems, each with a code editor and real tests. Write your solution, press Check, and see exactly which cases pass. Stuck? Hints reveal one at a time, and the solution unlocks once you have checked at least once. Your code and progress are saved in this browser.

Read the lesson

Meetings are [start, end] pairs in any order. Write merge_intervals(intervals) that merges every overlapping (or touching) pair and returns the result sorted by start.

merge_intervals([[8, 10], [1, 3], [2, 6], [15, 18]])
->  [[1, 6], [8, 10], [15, 18]]
def merge_intervals(intervals):
pass
Solution unlocks after your first Check

What Check runs

Your code runs first, then each of these lines. The output must match exactly.

RunsExpected output
merge_intervals([[8, 10], [1, 3], [2, 6], [15, 18]])[[1, 6], [8, 10], [15, 18]]
merge_intervals([[1, 4], [4, 5]])[[1, 5]]

More practice problems

All Python exercises · 50 practice problems