Design a Clinic Appointment System
🏥 What is a clinic appointment system? It is the software a clinic uses to fill its doctors' calendars. Doctors say when they are free, patients look for a free slot with the right kind of doctor and book it, and when someone cancels, the slot goes to the next patient who was waiting for it.
This problem looks like a plain booking system, and that is its trap. The booking itself is a few lines. What the interviewer wants to see is whether you pin down the rules around it: how long a slot is, what happens when two patients want the same one, whether a patient may be in two places at once, and who gets a slot when it frees up. Each of those answers turns into a field or a check, and the tests tell the variations apart.
The popular machine-coding version of this problem (often called FlipMed) asks for exactly this design: doctors register with a specialty and declare one-hour slots, patients search by specialty with results ranked by start time, book and cancel, and join a waitlist when a slot is taken. We cover all of it, and the ranking rule gets its own swappable class because that is the part those rounds like to change halfway through.
Requirements
"Design an appointment system for a clinic. Doctors publish their available slots, and patients search by specialty and book them."
Like most booking prompts, this hides the important decisions. These are the questions that change the design.
Clarifying Questions
You: How long is a slot? Can doctors pick any length?
Interviewer: Every slot is one hour. A doctor publishes a slot by its start time, in minutes. A doctor's own slots must not overlap.
You: When a patient searches, what do they search by, and in what order do results come back?
Interviewer: By specialty, like "Cardiology". Show every free slot of every doctor with that specialty, earliest first. If two slots start at the same time, order them by the doctor's name. We might rank by doctor rating later.
You: What happens if a patient asks for a slot that is already booked?
Interviewer: They join that slot's waitlist. If the booked patient cancels, the first patient on the waitlist gets the slot automatically.
You: Can one patient hold two appointments at the same time, say with two different doctors?
Interviewer: No. A patient can only be in one place. That also goes for the waitlist: you can't wait for a slot that overlaps something you already have or are already waiting for.
You: What does a booking return? Does a waitlisted patient get anything?
Interviewer: Every successful request gets an appointment id, booked or waitlisted. The patient can check the status of that id later, and it keeps the same id if it is promoted.
You: Do we need payments, reminders, or real dates?
Interviewer: No payments. Times are plain minutes. Reminders might come up later.
The fourth answer is the one people skip, and it decides a lot. "A patient's appointments never overlap" is a rule that has to be checked on every booking, so we will need a fast way to find a patient's current appointments. The fifth answer decides what a waitlist entry is: since a waitlisted request gets a real id that later becomes a booking, a waitlist entry is an appointment, not a separate kind of thing.
💡 Interview tip: When a prompt says "waitlist", ask two things straight away: is promotion automatic, and does the waiting patient keep any handle on their request? Both answers change the data model, and they are much cheaper to settle now than after you have written
cancel.
Final Requirements
- Doctors register with a unique name and one specialty.
- A doctor publishes one-hour slots by start time (minutes). A doctor's slots must not overlap; back-to-back slots are fine.
- Patients can list the free slots for a specialty: all free slots of all doctors with that specialty, sorted by start time, then by doctor name.
- A patient books a doctor's slot and gets an appointment id. If the slot is taken, the patient joins its waitlist and still gets an id, with status "waitlisted".
- A patient's booked and waitlisted appointments never overlap in time. A request that would break this is rejected.
- Cancelling a booked appointment frees the slot and gives it to the first patient on the waitlist, whose appointment becomes booked. Cancelling a waitlisted appointment removes it from the line.
- Anyone can look up an appointment's status and a patient's booked appointments.
- The ranking of search results will change (for example, by rating), so it must be swappable.
Out of scope
- Payments, insurance and invoices.
- Real dates, time zones and working hours.
- Reminders and notifications (we come back to this in Extensibility).
- Several receptionists booking at the same time (also in Extensibility).