Understanding Interval Problems: The Meeting Room Scheduler

Learn how interval problems work through a relatable meeting room scheduler analogy. Understand the fundamental technique for managing overlapping time slots efficiently.

Author

Mr. Oz

Date

Read

5 mins

Level 1

Meeting room scheduler visualizing overlapping time intervals as blocks on a timeline

Author

Mr. Oz

Date

Read

5 mins

Share

Imagine you're a meeting room coordinator at a busy office. Your job is simple: you receive a list of meeting requests, each with a start time and end time. Your goal is to combine overlapping meetings so the calendar looks clean.

For example, if someone books a meeting from 9 AM to 10 AM and another person books from 9:30 AM to 11 AM, these overlap. Instead of showing two separate meetings, you should combine them into one meeting from 9 AM to 11 AM.

The Naive Approach

Let's say you have these meeting requests written on sticky notes:

  • [1:00 PM - 3:00 PM]
  • [2:00 PM - 4:00 PM]
  • [5:00 PM - 6:00 PM]
  • [9:00 AM - 10:00 AM]

You could try to compare every meeting with every other meeting, but this gets messy quickly. With 10 meetings, you'd make 45 comparisons. With 100 meetings, that's nearly 5,000 comparisons!

This approach works, but it's inefficient and hard to implement correctly.

The Smart Approach: Sort First!

Here's the key insight that makes interval problems easy:

If you sort all meetings by their start time, overlapping meetings will always be next to each other.

Let's sort our meetings by start time:

  • [9:00 AM - 10:00 AM]
  • [1:00 PM - 3:00 PM]
  • [2:00 PM - 4:00 PM]
  • [5:00 PM - 6:00 PM]

Now look at them one by one, and merge if there's overlap:

  1. Start with [9:00 AM - 10:00 AM]. Add to your calendar.
  2. Next: [1:00 PM - 3:00 PM]. Does it overlap with [9:00 AM - 10:00 AM]? No. Add it.
  3. Next: [2:00 PM - 4:00 PM]. Does it overlap with [1:00 PM - 3:00 PM]? Yes (2:00 PM is before 3:00 PM). Merge into [1:00 PM - 4:00 PM].
  4. Next: [5:00 PM - 6:00 PM]. Does it overlap with [1:00 PM - 4:00 PM]? No. Add it.

Final result: [9:00 AM - 10:00 AM], [1:00 PM - 4:00 PM], [5:00 PM - 6:00 PM]

The Core Principle

Two intervals [a, b] and [c, d] overlap if:

c ≤ b (the second interval starts before the first one ends)

When they overlap, merge them into:

[a, max(b, d)] (keep the earlier start, take the later end)

  • Sort first — Arranges intervals in order
  • Compare adjacent — Only check neighbors, not everyone
  • Merge if needed — Combine overlapping intervals
  • Keep track — Maintain a result list as you go

Key Takeaways

  • Interval problems involve managing time ranges or numeric ranges
  • Sorting by start time is the key to efficient solutions
  • Two intervals overlap if the second starts before the first ends
  • When merging, keep the earlier start and the later end
  • This pattern applies to scheduling, merging, and many other real-world problems

All Levels