Design doctor appointment booking system + concurrency (semaphore, mutex)
Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-06-03
Problem Statement
Design a doctor appointment booking system that allows patients to book, cancel, and view appointments with doctors. Since multiple patients may attempt to book the same doctor and time slot concurrently, the system must ensure thread safety and avoid double-booking using synchronization primitives (semaphores, mutexes, locks).
Functional Requirements
1.Doctor Management
•Add/remove doctors.
•Maintain doctors’ availability (time slots).
2.Patient Appointment Management
•bookAppointment(patientId, doctorId, timeslot) → Books an available slot if not already taken.
•cancelAppointment(patientId, doctorId, timeslot) → Cancels an existing appointment.
•viewAppointments(doctorId) → Returns all booked slots for a doctor.
•viewPatientAppointments(patientId) → Returns appointments for a patient.
3.Concurrency
•Ensure atomic booking (only one patient can successfully book a given doctor & slot).
•Prevent race conditions in high-traffic scenarios (e.g., multiple patients booking the same popular doctor at 10 AM).
Non-Functional Requirements
•Thread Safety → Use mutex or synchronized blocks to avoid race conditions.
•Scalability → Support many doctors and patients concurrently.
•Performance → Ensure low latency booking under contention.
Concurrency Control
•Use mutex/lock per doctor’s schedule to ensure consistent updates.
•Use semaphores if slots per doctor are bounded (e.g., only N patients per timeslot).
•Implement atomic operations for slot updates.
Edge Cases
•Two patients trying to book the same doctor and timeslot → only one succeeds.
•Cancel appointment for a non-existing booking → should fail gracefully.
•Book appointment for a doctor with no availability → should fail.
•Doctor removed while having appointments → handle cleanup of bookings.
Design Hints
Classes & Interfaces
•Doctor → id, name, specialization, availableSlots.
•Patient → id, name, history.
•Appointment → doctorId, patientId, timeslot.
•AppointmentManager
•bookAppointment(patientId, doctorId, timeslot)
•cancelAppointment(patientId, doctorId, timeslot)
•viewAppointments(doctorId)
•viewPatientAppointments(patientId)
Concurrency Handling
•Use ConcurrentHashMap<doctorId, DoctorSchedule> to store doctor schedules.
•Each DoctorSchedule can use a ReentrantLock / synchronized block to prevent conflicting updates.
•Alternatively, use Semaphore per timeslot if each slot can take limited patients.
Example Flow
1.Doctor D1 has available slots: 10 AM, 11 AM.
2.Patient P1 calls bookAppointment(P1, D1, 10AM) → success.
3.At the same time, Patient P2 calls bookAppointment(P2, D1, 10AM) → blocked by lock, then fails as slot is already taken.
4.Patient P1 cancels → slot becomes available again.
5.Another patient can now book that slot.
Evaluation Areas
•Correct OOP modeling of doctors, patients, and appointments.
•Proper synchronization (mutex, semaphore, locks) to ensure thread safety.
•Handling of edge cases like double-booking, cancellations, and concurrent requests.
•Extensibility → easy to add new features like reminders, waitlists, or priority bookings.