DSA Tracker

DSA Tracker

Topic Lesson

Learn Intervals from scratch

A complete 4-step path: handwritten notebook primer, animated visualizer, speed check quiz, and compiler practice.

What is Intervals

notebook note

An interval is a continuous range of numbers defined by two values: a start point and an end point. Think of a calendar event booked from two to four o'clock, or a cut segment on a ruler. Because each interval covers every number between its boundaries, two intervals can sit apart with empty room between them, touch at an edge, or overlap across a shared span of numbers.

When to reach for it

Reach for intervals when the input consists of start and end pairs representing time slots, schedules, ranges, or geometric segments. Phrasings asking to merge overlapping blocks, insert a new meeting into a busy calendar, find the minimum number of conference rooms needed, or count how many intervals must be removed to eliminate overlaps all point directly to interval patterns. Whenever problems involve resource contention over time, think intervals.

How to think about it

The opening move is almost always sorting the intervals by their start times, or occasionally by their end times. Once ordered, compare the current interval with the previous one. If the new start time is less than or equal to the previous end time, the two intervals collide; merge them by stretching the previous end time to the maximum of both ends. If they do not collide, the previous interval is finished, so append it to the result and start tracking the new one. For room counts, split intervals into separate start and end events.

Operation costs

measured big-O
OperationTime
sort intervals by start or end timeO(n log n)
merge sorted intervals in a single passO(n)
track active meetings using a min-heapO(n log n)

Common mistakes to avoid

  • Merging two overlapping intervals by taking the second interval end without using the maximum of both ends, which shrinks an interval when the first one completely swallowed the second.
  • Treating intervals that touch at the exact same boundary point as disjoint when the problem statement defines boundaries as closed and inclusive.
  • Forgetting to append the final merged interval to the output list after the iteration loop finishes.
Step 1 of 4