Grokking the Coding Interview: Patterns for Coding Questions
Vote

0% completed

Introduction to Merge Intervals Pattern

You are given a list of meetings, each with a start time and an end time. Combine every group of meetings that overlap into a single block.

[[1, 4], [2, 5], [7, 9], [8, 10]]   becomes   [[1, 5], [7, 10]]

The direct approach compares every meeting against every other one. It merges any pair that touches, then repeats until nothing changes. That is O(N²) at best, and the repeated passes make it worse.

The fix is one line of preparation. Sort the meetings by start time.

Once sorted, a meeting can only overlap the block you are building

.....

.....

.....

Like the course? Get enrolled and start learning!