Design a Doctor Appointment Booking System with Slot Locking and Region Search
Company: Headway
Role: Software Engineer
Category: System Design
Difficulty: hard
Interview Round: Onsite
Design an appointment booking system for doctors. Patients find doctors, see open time slots, and book, reschedule or cancel appointments. The design must cover:
- create, read, update and delete operations for appointments;
- locking, so that when different patients try to book the same doctor at the same time, only one of them gets the slot;
- doctor search by region, with ranking of the results and paginated result pages.
### Clarifying Questions
- How many doctors and patients are there, and what are the peak booking and search rates?
- Who defines availability: do doctors publish working hours that the system cuts into slots, or do they create slots one by one? Are all appointments the same length?
- Should a slot be held for a patient while they fill in booking details, and for how long?
- What does "region" mean for search: a city, a postal code, or a radius around the patient?
- What should ranking reward: distance, earliest availability, how well the doctor matches the search, or something else?
- What are the rules for cancelling and rescheduling, such as a cutoff before the appointment?
### Part 1: Appointments and their CRUD APIs
Define the data model for doctors, their availability and appointments, and the APIs to create, read, update (reschedule) and cancel an appointment.
```hint Separate availability from bookings
Decide whether an open slot is a stored row or something computed from working hours minus existing appointments, and what each choice means for updates.
```
#### What This Part Should Cover
- Entities with the fields and indexes the booking and lookup paths need.
- API shapes, including how a client retries a booking safely.
- Appointment states and the transitions between them.
### Part 2: Two patients, one slot
Two patients try to book the same doctor for the same time. Explain how you lock, or otherwise guarantee, that exactly one booking succeeds. Include rescheduling, which frees one slot and takes another.
```hint Where the rule lives
Ask which component can enforce "one active appointment per doctor and time" even when two application servers handle the two requests at the same moment.
```
#### What This Part Should Cover
- A concrete concurrency mechanism and why it is correct under parallel requests.
- Holds that expire, and what happens when a patient abandons the flow.
- Behavior under failures: timeouts, retries and a crash between steps.
### Part 3: Search doctors by region, ranked and paginated
Patients search for doctors in a region. Design the search: how doctors are indexed by location, how results are ranked, and how pages of results are returned.
```hint Pages that move
Rankings can change between page requests as slots get booked. Decide how page 2 stays consistent with page 1.
```
#### What This Part Should Cover
- The index and query for location-based search.
- A ranking approach and the inputs it needs.
- A pagination scheme and how it behaves when results change.
- How the search index stays in sync with doctor and availability data.
### What a Strong Answer Covers
- Requirements and estimates that drive the choices.
- A data model in which the double-booking rule is enforced by a single authority.
- Read and write paths for booking and for search, with caching only where staleness is safe.
- Failure handling and observability for booking conflicts and index lag.
### Follow-up Questions
- A popular doctor opens next month's calendar and thousands of patients try to book at once. What changes?
- Appointments can have different lengths. How do you prevent overlapping bookings?
- How would you send reminders before each appointment without sending any twice?
- How would you support doctors who practice at several locations?
Overview: Design an appointment booking system for doctors that supports creating, reading, rescheduling and cancelling appointments, guarantees that two patients cannot book the same doctor and time, and searches doctors by region with ranked, paginated results. It tests data modeling, concurrency control, geo search and pagination design.
Read the full Headway Software Engineer interview experience this question came from