Interval Scheduling — O(n²) Blowup Led to 30s Timeouts
Naive O(n²) interval comparison caused 30-second timeouts on imported Outlook calendars.
- Interval scheduling finds max non-overlapping meetings using earliest finish time greedy
- Minimum meeting rooms: sort starts, track active end times with a min-heap
- Merge intervals: sort starts, merge consecutive overlaps in a single pass
- All three run O(n log n): the sort dominates
- Production trap: assuming sorting by start time works for max intervals — it does not
You have a list of meetings, each with a start and end time. Two problems: (1) What is the maximum number of non-overlapping meetings you can attend? (2) What is the minimum number of rooms needed to host all meetings simultaneously? Both have elegant greedy solutions.
Interval scheduling appears in every operating system (process time slots), every calendar application (meeting conflict detection), and virtually every resource allocation problem. The three variants — maximise non-overlapping intervals, minimise rooms needed, merge overlapping intervals — are among the most frequently asked algorithmic interview problems at top companies.
All three have elegant O(n log n) greedy solutions, but the correctness argument for each is different. Maximum non-overlapping uses earliest deadline first with an exchange argument. Minimum rooms uses a priority queue sweep. Merge intervals is just careful iteration. Knowing all three and why each works — not just what to code — is what distinguishes thorough preparation.
Problem 1 — Maximum Non-Overlapping Intervals
Greedy insight: always pick the interval that finishes earliest. This leaves maximum room for future intervals.
Problem 2 — Minimum Meeting Rooms
Use a min-heap tracking end times of active meetings. For each meeting start, if the earliest-ending active meeting has ended, reuse its room.
Problem 3 — Merge Overlapping Intervals
Sort by start time, merge consecutive overlapping intervals.
Why Earliest Finish Time is Optimal
Claim: picking the interval with the earliest finish time is always in some optimal solution. Proof: suppose optimal starts with interval A instead of E (earliest finish). Replace A with E — since E finishes earlier, this can only improve future compatibility. By induction, greedy is optimal.
When Greedy Fails — Weighted Interval Scheduling
The earliest finish time greedy does NOT work when intervals have weights (values). A short, high-value interval might be later than a long, low-value one. For weighted interval scheduling you need dynamic programming: 1. Sort by end time 2. Use DP[i] = max(weight[i] + DP[p(i)], DP[i-1]) where p(i) is the last interval non-overlapping with i. This is O(n log n) with binary search for p(i).
Naive Interval Scheduling Caused 30-Second Timeouts
- Always profile with production-like data volumes before assuming algorithm choice is irrelevant.
- Interval scheduling problems often feel small — but calendars grow fast.
- When you see an O(n^2) algorithm in an interview solution, that's a red flag.
Key takeaways
Common mistakes to avoid
4 patternsSorting by start time for max non-overlapping intervals
Using '<' instead of '<=' when checking overlap for meeting rooms
Merging intervals without clarifying overlap definition
Applying greedy to weighted interval scheduling
Interview Questions on This Topic
Why does sorting by end time give the optimal solution for interval scheduling?
Frequently Asked Questions
That's Greedy & Backtracking. Mark it forged?
3 min read · try the examples if you haven't