download

Meetings 9–10 and 10–11 fit one room. Why do you get two?

Algorithms~15 minReviewed 1 Nov 2026Free · No sign-in

Question

A support team books meeting rooms. A meeting (start, end) holds its room from start up to end, but not at end. So a 9–10 meeting frees its room at 10, and a 10–11 meeting can use it.

The function below should return the fewest rooms needed. For 9–10 and 10–11 it returns 2. Find the bug and fix it. Then give the answer for the team's day:

"PYTHON"
team = [(9, 10), (10, 11), (9, 12), (11, 13), (12, 14)]
"CODE · PYTHON"
def rooms_needed(meetings):
    events = []
    for start, end in meetings:
        events.append((start, False))  # False: a meeting starts
        events.append((end, True))     # True: a meeting ends
    events.sort()
    rooms = most = 0
    for _, is_end in events:
        rooms += -1 if is_end else 1
        most = max(most, rooms)
    return most


print(rooms_needed([(9, 10), (10, 11)]))  # 2, but one room is enough
Show hints (3)
  1. "LOOK AT TIME 10"

    Write down the sorted events for [(9, 10), (10, 11)]. Two events happen at 10. Which one comes first, and what does the counter show between them?

  2. "HOW TUPLES SORT"

    Tuples sort by the first item, then by the second. What does Python say for False < True? So at the same time, which comes first: a start or an end?

  3. "THE FIX"

    At a tie, the room must be freed before it is taken again. Use (time, -1) for an end and (time, 1) for a start, so a plain sort puts ends first.

Show checked explanation

The meetings are half-open: a room is busy from start up to, but not including, end. The code sorts a start before an end at the same time, because False < True. So at 10 it takes the room before it frees it. Encode ends as -1 and starts as 1 so ends sort first. The team needs 2 rooms.

"SOLUTION · PYTHON"
def rooms_needed(meetings):
    events = []
    for start, end in meetings:
        events.append((start, 1))  # a meeting takes a room
        events.append((end, -1))   # a meeting frees its room
    events.sort()  # same time: -1 sorts first, so free before take
    rooms = most = 0
    for _, change in events:
        rooms += change
        most = max(most, rooms)
    return most


team = [(9, 10), (10, 11), (9, 12), (11, 13), (12, 14)]
print(rooms_needed([(9, 10), (10, 11)]))
print(rooms_needed(team))
print(rooms_needed([]))
"OUTPUT"
1
2
0

Code run on CPython 3.12 against the examples above. Checked by an editor other than the author.

Walkthrough

Tuples sort by time first, then by the second item. In the old code that is False for a start and True for an end, and False < True. So at the same time, starts come first.

Here are the team's events in sorted order, with the room count after each one:

timeold orderroomsfixed orderrooms
9start, start1, 2start, start1, 2
10start, end3, 2end, start1, 2
11start, end3, 2end, start1, 2
12start, end3, 2end, start1, 2
13end1end1
14end0end0

The old code peaks at 3, three times. But at 10 the 9–10 meeting has already left. Three meetings never run at once. The fixed code peaks at 2, which is the answer.

Why must ends go first? A meeting (start, end) is in its room at time t when start <= t < end. At 10, the 9–10 meeting is out and the 10–11 meeting is in. With ends first, the count at each time only drops, then rises. So it never shows a false peak.

With (time, 1) for a start and (time, -1) for an end, a plain sort() does this, because -1 sorts before 1. A zero-length meeting like (10, 10) then frees its room before taking it, so it adds no room. That is right: it covers no time.

Sorting costs O(n log n). The pass after it is O(n).

Common mistakes

  • Sorting with key=lambda e: e[0] only. The sort is stable, so ties keep the order you appended them in. [(10, 11), (9, 10)] gives 2, but the same meetings in reverse order give 1.
  • In the version with two sorted lists (all starts, all ends), opening a new room when start <= ends[j]. A meeting at 10 then can't reuse the room freed at 10, and the team gets 3.
  • Counting overlapping pairs instead of the peak. The team's day has 4 overlapping pairs, but at most 2 meetings ever run at once. Rooms depend on the peak.

Follow-up questions

  • "Now a meeting also holds its room at the end time. What changes?" A 9–10 and a 10–11 meeting now share the moment 10, so process starts before ends at a tie. Sort by (time, -change). The team then needs 3 rooms. A zero-length meeting like (10, 10) now takes a room for that moment.
  • "Which meeting goes to which room?" Sort the meetings by start. Keep a min-heap of (end, room) for busy rooms. If the earliest end is at or before the new start, pop it and reuse that room. Otherwise open a new room. The team's day opens 2 rooms.
  • "Times are whole minutes in one day, and there are millions of meetings." Skip the sort. Use 1,441 counters, one per minute from 0 to 1,440. Add 1 at each start and subtract 1 at each end. The largest running sum is the answer. That is O(n + 1,440) time.

What a strong answer covers

Asks whether a meeting holds its room at the end time before writing code. Explains why ends go first at a tie for half-open meetings. Tests back-to-back meetings, a zero-length meeting and an empty list.

Open in interactive practice deck →

Original OfferVibe question. Topic idea from Coding Interview Patterns (Alex Xu and Shaun Gunawardane, 2024); text, examples and code are our own.
© 2026 OfferVibe · Text CC BY-NC-SA 4.0 · code MITFree to share and adapt with credit, not for commercial use. Code samples are free for any use.