Module F-10·14 min read

Merging overlapping intervals, meeting-room scheduling, and sweeping sorted events in one pass.

JJS
Written by Jatin Jain Saraf · Senior Software Engineer

Module F-10 — Intervals and Line Sweep

What this module covers: So far, this course has dealt with single points or contiguous runs within one sequence. This module shifts to reasoning about many ranges at once — meetings on a calendar, time periods, numeric bands — and the two techniques that make sense of them efficiently: sorting-and-merging, and sweeping through start/end events in order while tracking a running count.


Representing Intervals

An interval is just a pair — a start and an end:

typescript

The moment you have a collection of these, two questions come up constantly: which ones overlap and should be merged into one? and how many are active at the same time, at peak? Both questions have the same starting move: sort first, so overlaps and boundaries become adjacent and easy to detect in a single pass.


Merging Overlapping Intervals

Sort by start time, then walk through once: if the current interval's start is within the previous interval's range, merge them by extending the end; otherwise, start a new interval.

typescript

Sorting costs O(n log n) (Modules P-9/P-10), and the merge pass afterward is a single O(n) walk — no interval is ever compared against more than its immediate predecessor, because sorting has already guaranteed that any interval it could overlap with is sitting right next to it. Without sorting first, you'd be back to comparing every interval against every other one — O(n²).


Line Sweep: Counting Overlaps at Any Moment

A different question: what is the maximum number of intervals active at the same time? — the classic "minimum meeting rooms needed" problem. The insight is to stop thinking about intervals as units and instead think about the individual start and end events, sorted in time order, sweeping through them like a line moving left to right across a timeline.

typescript

Separating starts and ends into two sorted arrays and sweeping through both with two pointers (Module F-8) turns "how many meetings overlap at the busiest moment" into a single pass: every start event increments a running count, every end event decrements it, and the peak value the counter ever reaches is the answer. This is the line sweep technique — process discrete events in time order, maintaining a running total, rather than checking every pair of intervals for overlap directly (which would be O(n²)).

Sign in to keep reading

The rest of this module is free — sign in with Google to unlock it and track your progress.

Sign in & Register

Discussion

0

Join the discussion

Loading comments...

© 2026 Jatin Jain Saraf (JJS). All rights reserved.