Design a meeting room scheduler
medium 80 minBook 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
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
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
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
criticalA wish that lies around a booking, or over its start, is let through.
Wrongif b.start <= start and end <= b.end: return FalseRightif b.start < end and start < b.end: return FalseA public check before booking
criticalThe answer is old when it arrives. Two callers are both told yes.
Wrongif school.is_available(room, slot): school.book(room, slot)Rightbooking = school.book(request) # looks and placesMoving by cancel and book
majorThe second half can fail, and the old place is gone.
Wrongschool.cancel(id, who)school.book(new)Rightmoved = school.move(id, who, slot)Availability as a field
majorA second truth that will disagree with the bookings.
Wrongroom.is_free = FalseRightcalendar.conflict(slot) is NoneAdding 24 hours for tomorrow
minorOn two nights a year it is an hour off.
Wrongnext_utc = start_utc + timedelta(hours=24)Rightnext_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.