AlgoViz

Design a meeting room scheduler

medium 80 min

Book a room for a slot, a number of people and some equipment, without ever booking one room twice: half-open slots, a sorted calendar for each room, rules that can be added, one lock, and then repeats, time zones and empty rooms.

Video lesson · Chapter 1 of 6

Make a prediction

0:00 / 4:39

See it run

Objects of the music school: calls travel, calendars change, and the matching lines light up.

Operations

Book a room

Lee wants a room for two from 10:30 to 11:30. Attic is taken by Asha from 10 to 11.

Step 1 of 6

A wish

LeecallerschoolSchedulerlockfreebookings1AtticRoomCalendar1st10:00-11:00BlueRoomCalendar1stnoneCellarRoomCalendar1stnonebook(10:30, 2 people)

Lee's request arrives. The scheduler knows one booking, Asha's on Attic.

Step 1: A wish

def book(self, request):    self._check_rules(request)    candidates = self._choice.order(self._rooms.values(), request)    with self._lock:        for room in candidates:            calendar = self._calendars[room.id]            if calendar.conflict(request.slot) is None:                booking = Booking(next(self._ids), room.id, request.owner, request.slot)                calendar.add(booking)                self._bookings[booking.id] = booking                return booking    return None
Cost
O(rooms log n)
Guarantee
One booking
One list of all
reads the whole history

Sorting rooms happens before the lock, looking and placing inside it.

In the real world

The same meeting room scheduler doing real jobs. Pick one and step through it.

Lee booked Blue for 10:30 and does not check in. Grace is 15 minutes.

Step 1 of 4

10:40

MaxcallerBlueRoomCalendarnow10:40booking 2BookingownerLeeherenountil10:45book 10:45none

Max asks for Blue. Lee's booking still holds, because the grace ends at 10:45.

Step 1: 10:40

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • Testing only whether it lies inside

    critical

    A wish that lies around a booking, or over its start, is let through.

    Wrong
    if b.start <= start and end <= b.end:    return False
    Right
    if b.start < end and start < b.end:    return False
  • A public check before booking

    critical

    The answer is old when it arrives. Two callers are both told yes.

    Wrong
    if school.is_available(room, slot):    school.book(room, slot)
    Right
    booking = school.book(request)   # looks and places
  • Moving by cancel and book

    major

    The second half can fail, and the old place is gone.

    Wrong
    school.cancel(id, who)school.book(new)
    Right
    moved = school.move(id, who, slot)
  • Availability as a field

    major

    A second truth that will disagree with the bookings.

    Wrong
    room.is_free = False
    Right
    calendar.conflict(slot) is None
  • Adding 24 hours for tomorrow

    minor

    On two nights a year it is an hour off.

    Wrong
    next_utc = start_utc + timedelta(hours=24)
    Right
    next_local = start_local + timedelta(days=1)

Interview tips

  • Open with

    • The end is not included.
    • For each room, no overlap.
    • Four public verbs.
  • Reject aloud

    • An hour is a number.
    • A piano is a word in a set.
    • Availability is an answer.
  • Prove it

    • Trace touching slots.
    • Trace a move onto itself.
    • Compare with a set of minutes.
  • Never

    • A check before booking.
    • Cancel and then book.
    • An interval tree without a reason.