← All Exercises

Merge Overlapping Intervals

intermediatejavascript~25min

Background

Interval merging is used in calendar scheduling, resource allocation, and computational geometry applications.

Problem

Write a function that takes an array of intervals (pairs of start and end numbers) and merges all overlapping intervals, returning a sorted list of non-overlapping intervals.

Input

An array of intervals, each represented as [start, end] with start <= end.

Output

An array of merged non-overlapping intervals sorted by start time.

Sample tests

Sample 1

Input: [[1,3],[2,6],[8,10]]
Expected: [[1,6],[8,10]]

Your solution

Learning objective

Learn interval processing, sorting by custom comparators, and greedy merging strategies.