78. Design Doctor Appointment Booking app like Practo

Design Doctor Appointment Booking app like Practo
You are required to build an in-memory application that lets patients connect to doctors and book appointments for a single day.

Doctors can declare their availability in terms of slots for that day only. Patients can search available slots by speciality, book appointments, and cancel appointments.

Rules

  • Doctor specialities are restricted to: Cardiologist, Dermatologist, Orthopedic, General Physician.
  • All availability and bookings are for the current day only (no multi-day support).
  • Doctors cannot provide overlapping slots.
  • A patient can book multiple appointments in a day, but cannot book two appointments with two different doctors in the same time slot.
  • Waitlist feature:
    • If a patient tries to book a slot for a specific doctor that is already booked, the patient can opt into waitlist.
    • If the confirmed appointment is cancelled later, the first patient in the waitlist for that doctor+slot gets the appointment.

Time Format

  • The day is divided into time slots of 60 mins each.
  • Use 24-hour time in HH:MM format.
  • Each availability slot is represented as a string HH:MM-HH:MM (example: 09:00-10:00).
  • Valid slot start times are 09:00 through 20:00 (inclusive), and valid slot end times are 10:00 through 21:00 (inclusive).

Class to Implement

  • Implement a class named DoctorAppointmentSystem.
  • Do not use any database or NoSQL store; use in-memory data structures only.

Method Signatures

void registerDoctor(String doctorName, String speciality, List<String> slots)
  • doctorName is non-empty and uniquely identifies the doctor.
  • Speciality and slots will always be valid.
  • speciality must be one of: Cardiologist, Dermatologist, Orthopedic, General Physician.
  • If same doctor is added again then their slots and speciality are overwritten.
  • slots contains strings in time format as explained above.

String bookAppointment(String bookingId, String patientId, String doctorName, String startTime, boolean addToWaitlistIfBooked)
  • patientId is a non-empty identifier.
  • doctorName must be registered.
  • startTime is a slot start time in HH:MM (example: 12:00).
  • The doctor must have declared availability for the slot starting at startTime.
  • A patient cannot have another confirmed appointment in the same slot time with a different doctor. return "Slot already booked"
  • If the slot is free, return: BOOKED.
  • If the slot is already booked:
    • If addToWaitlistIfBooked is true, add to waitlist and return: Added to the waitlist.
    • If addToWaitlistIfBooked is false, return: Slot already booked.
List<String> showAvailabilityBySpeciality(String speciality)
  • Return all currently available (not booked) slots for doctors of the given speciality.
  • rank slots by slot start time ascending (tie-break by doctorName ascending).
  • Each returned entry must be a string like: Dr.<doctorName>: (<HH:MM-HH:MM>).
  • If no slots are available for a speciality, return empty list.

List<String> cancelBooking(String bookingId)
  • Cancel the booking with id bookingId, if bookingId is in waitlist then remove it.
  • Return a list of messages (to support waitlist auto-confirmation):
    • Always include: Booking Cancelled if cancellation succeeds.
    • If someone is promoted from waitlist, also include: Booking confirmed for Booking id: <id>.
  • If bookingId does not exist or is not cancellable, return: ["Invalid booking id"].

List<String> showAppointmentsBooked(String userName)
  • userName can be a patient id or a doctor name.
  • Return all confirmed appointments for that user for the day.
  • Each entry must be a string like: Booking id: <id>, Dr <doctorName> <startTime> (for patients),
    Booking id: <id>, <patientId> <startTime> for doctors.

Constraints

  • 1 ≤ number of doctors
  • Waitlist order is FIFO per (doctorName, startTime).

Examples

Example 1: Register doctors and view availability by speciality

registerDoctor(doctorName="Curious", speciality="Cardiologist", slots=List.of("12:00-13:00","16:00-17:00"))

registerDoctor(doctorName="Alpha", speciality="Cardiologist", slots=List.of("09:00-10:00"))

showAvailabilityBySpeciality(speciality="Cardiologist")
Output: List.of("Dr.Alpha: (09:00-10:00)","Dr.Curious: (12:00-13:00)","Dr.Curious: (16:00-17:00)")

Example 2: Book an appointment and verify availability + booked list

bookAppointment(bookingId="b1", patientId="PatientA", doctorName="Alpha", startTime="09:00", addToWaitlistIfBooked=false)
Output: BOOKED

showAvailabilityBySpeciality(speciality="Cardiologist")
Output: List.of("Dr.Curious: (12:00-13:00)","Dr.Curious: (16:00-17:00)")

showAppointmentsBooked(userName="PatientA")
Output: List.of("Booking id: b1, Dr Alpha 09:00")

showAppointmentsBooked(userName="Alpha")
Output: List.of("Booking id: b1, PatientA 09:00")

Example 3: Waitlist promotion after cancellation

bookAppointment(bookingId="b2", patientId="PatientB", doctorName="Curious", startTime="12:00", addToWaitlistIfBooked=false)
Output: BOOKED

bookAppointment(bookingId="b3", patientId="PatientC", doctorName="Curious", startTime="12:00", addToWaitlistIfBooked=true)
Output: Added to the waitlist

cancelBooking(bookingId="b2")
Output: List.of("Booking Cancelled","Booking confirmed for Booking id: b3")

showAppointmentsBooked(userName="PatientC")
Output: List.of("Booking id: b3, Dr Curious 12:00")

showAppointmentsBooked(userName="Curious")
Output: List.of("Booking id: b3, PatientC 12:00")




Please use Laptop/Desktop or any other large screen to add/edit code.