Design a Hospital Appointment System (with SSE)
Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-03-25
Problem Statement
Design a hospital appointment system that allows patients to book, cancel, and view appointments with doctors. The system should also support real-time updates using SSE so that patients and doctors are notified of changes instantly. Focus is on OOP design, modularity, concurrency, and event-driven updates.
Functional Requirements
1.Patient Operations
•Book an appointment with a doctor at a specific date and time.
•Cancel or reschedule an existing appointment.
•View upcoming and past appointments.
2.Doctor Operations
•View their schedule.
•Approve or reject appointments (optional).
3.Real-Time Notifications (SSE)
•Patients and doctors should receive real-time updates for booking, cancellation, or rescheduling events.
•Events sent via Server-Sent Events (SSE) or similar streaming mechanism.
4.Availability Management
•Maintain doctors’ availability slots.
•Prevent double booking of the same time slot.
5.Optional Features
•Support multiple specializations and hospital branches.
•Waitlist management for fully booked slots.
•Analytics for appointment trends.
Non-Functional Requirements
•Scalability: Handle many patients and doctors simultaneously.
•Concurrency: Ensure thread-safe booking and cancellation.
•Low Latency: Real-time notifications via SSE.
•Consistency: Avoid double booking and maintain accurate schedules.
Edge Cases & Constraints
•Booking at overlapping times → must reject or handle conflicts.
•Cancelling non-existent appointments → error handling.
•SSE clients disconnecting → implement reconnection and state sync.
•High-frequency updates → avoid flooding clients with events.
Design Hints
•Key Classes / Interfaces:
•Patient → contains patient info, appointments.
•Doctor → contains doctor info, availability, schedule.
•Appointment → contains patient, doctor, time, status.
•AppointmentService → handles booking, cancellation, rescheduling.
•SSEManager → handles subscription of patients/doctors to real-time events and pushes updates.
•Data Structures:
•Map<DoctorId, TreeSet<Appointment>> → to efficiently check availability and schedule.
•PriorityQueue / SortedSet → for upcoming appointments per doctor or patient.
•Queue<Event> → for SSE event delivery to subscribers.
•Design Patterns:
•Observer Pattern → for notifying subscribers of changes.
•Singleton → for centralized AppointmentService or SSEManager.
•Command Pattern (Optional) → for undo/redo of appointment operations.
Example Flow
1.Patient requests bookAppointment(patientId, doctorId, datetime).
2.AppointmentService checks doctor availability → creates Appointment.
3.Appointment added to doctor and patient schedules.
4.SSEManager pushes event → patient and doctor clients receive real-time notification.
5.Patient cancels appointment → schedules updated, SSE notifications sent.
Evaluation Areas
•Clean class design and separation of concerns.
•Correct availability tracking and conflict resolution.
•Thread safety for concurrent bookings and cancellations.
•Efficient event-driven notification mechanism using SSE.
•Extensibility to support multiple branches, specializations, and analytics.