TRT
Sign in
mediumAdvancedcoding

How many meeting rooms do you need?

Solve the minimum rooms problem with a sweep over start and end events or a min-heap of end times, and handle ties correctly.

TRT practice prompt — not a verified question from a named employer.

The Runtime Theory Team1 min read
Learn it firstIntervals and sweepingPro — members onlySort intervals by start and most interval problems become a single pass: merging overlaps, inserting a new interval, or sweeping through start and end events to count how many intervals overlap at once.

A strong answer

The minimum number of rooms equals the maximum number of meetings in progress at any single moment: that many are clearly needed, and greedily reusing any free room never needs more.

Sweep: turn each meeting [start, end) into a +1 event at start and a −1 event at end, sort by time with ends before starts at equal times (so a room freed at 10 can be reused at 10), and walk the events keeping a running count. The largest count is the answer. Sorting dominates: O(n log n).

Heap: sort meetings by start. Keep a min-heap of end times of meetings currently holding rooms. For each meeting, if the earliest end in the heap is at or before its start, that room is free: pop it. Push the meeting's end. The heap's maximum size, which is its final size here, is the number of rooms. Also O(n log n), and it naturally extends to assigning rooms: store (end, room id) and reuse the popped room's id.

If times are small integers, such as minutes in a day, a difference array indexed by time replaces sorting: +1 at each start, −1 at each end, and the maximum prefix sum is the answer in O(n + T).

The tie rule depends on the interval convention: for half-open intervals, touching meetings don't conflict; for closed ones, they do, and starts must be processed before ends.

What the interviewer is listening for

  • The reduction to maximum overlap, with a reason it is optimal.
  • One correct O(n log n) solution with ties handled deliberately.
  • Awareness of the heap version for room assignment, and of the integer-time shortcut.

Common mistakes

  • Comparing all pairs of meetings.
  • Getting the tie order wrong, which overcounts rooms by one.
  • Sorting by end time for this problem, which doesn't give the overlap count.

This answer walks

Practice follow-ups

  1. 01How would you also assign each meeting to a specific room?
  2. 02Meetings arrive one at a time and must be accepted or rejected immediately with r rooms. What changes?
  3. 03How would you merge overlapping meetings into busy blocks?
  4. 04What if times are small integers, such as minutes in a day?

Not started

Sign in to save your learning progress.

Sign in to save