Design a Hotel Booking System
Intermediate · 22 min read · Low-Level Design, Case Studies: Business Systems
Half-open date ranges, O(log n) availability, per-room locks and per-night pricing.
Hotel booking looks like movie booking, but the resource is different: a seat is booked for one show; a room is booked for a range of nights. That turns availability into an interval-overlap problem, and it's where most candidates slip — off-by-one on checkout day, O(n) scans of every booking, or double bookings under…
What the interviewer expects
The full design in this lesson is longer than anything you'd write in one round. It's there so you understand the edge cases and can answer follow-up questions. In the round itself, aim for this:
1. Requirements
2. The date-range model: half-open intervals
Represent a stay as [checkIn, checkOut) — the nights from check-in up to, but not including, checkout:
3. Class design
4. Availability per room: a sorted calendar
Each room keeps its confirmed bookings in a TreeMap keyed by check-in date. Because a room's bookings never overlap each other, only one booking can possibly clash with a new range: the last one that starts before the new range ends.
5. Booking by type, safely under concurrency
The lock is per room: guests booking different rooms never wait for each other, and each room's check-and-insert is atomic. This is the same "candidates + atomic claim" shape as parking spots and drivers.
6. Pricing per night: Strategy + Decorator
Pricing per night (not per stay) is what makes weekend, seasonal and event pricing composable: a festival surcharge is another decorator; a long-stay discount is a decorator on the quote. Note which nights count as "weekend": a Friday-to-Sunday stay has Friday and Saturday nights.
7. Cancellation as a strategy
Two details that matter: deadlines are computed in the hotel's time zone (a guest in another country still loses free cancellation at 14:00 hotel time minus 48 hours), and the booking's own status transition (CONFIRMED → CANCELLED) is synchronized, so a double cancel fails instead of freeing the room twice.
8. Patterns used
9. Extensions
Interview questions
- How do you check whether a room is free for a date range efficiently?
- Why model stays as half-open ranges?
- Two guests book the last deluxe room for overlapping dates at the same moment. What happens?
Further reading
Key takeaways
- Model stays as half-open date ranges; overlap is two comparisons.
- Per-room sorted calendars give O(log n) availability checks; only one neighbour can clash.
- Book by type with candidate rooms and an atomic per-room claim — no global lock.
- Price per night with composable decorators; compute deadlines in the hotel's time zone.
Previous: Design a Food Delivery System · Next: Design a Digital Wallet