488. Course Booking System with Student Limits
Design College Course Booking System for Students

Implement an in-memory college course booking system with student limits. Students can browse courses, book one seat per course, cancel bookings, and view their booking history. Each course has a fixed capacity, and each student may have at most N confirmed bookings at a time.

Method Signatures

CourseBookingSystem(int N)
  • Create an empty system with the same fixed booking limit N for every student.
boolean registerStudent(String studentId, String name)
  • Register the student. Return true on success or false if studentId already exists.
boolean addCourse(String courseId, String title, String instructorName, int capacity)
  • Add a course with the supplied details. Return true on success or false if courseId already exists. Course details cannot be changed.
List<String> searchCourses(String keyword)
  • Find courses whose titles contain keyword, ignoring ASCII letter case. An empty keyword matches every course. Include full courses.
  • Return rows as "courseId,title,instructorName,capacity,availableSeats", sorted by courseId.
String bookCourse(String bookingId, String studentId, String courseId)
  • Reserve one seat and create a CONFIRMED booking. Return "BOOKED" on success.
  • Otherwise return the first applicable error in this order: "BOOKING_ID_EXISTS", "STUDENT_NOT_FOUND", "COURSE_NOT_FOUND", "ALREADY_BOOKED", "COURSE_LIMIT_REACHED", "COURSE_FULL".
  • ALREADY_BOOKED means the student already has a confirmed booking for this course. COURSE_LIMIT_REACHED means the student already has N confirmed bookings.
String cancelBooking(String studentId, String bookingId)
  • Cancel the student's confirmed booking, retain it with status CANCELLED, and release its seat and one place within the student's limit. Return "CANCELLED".
  • Otherwise return the first applicable error in this order: "STUDENT_NOT_FOUND", "BOOKING_NOT_FOUND", "NOT_BOOKING_OWNER", "ALREADY_CANCELLED".
List<String> getStudentBookings(String studentId)
  • Return all of the student's bookings, including cancelled ones, as "bookingId,studentId,courseId,status", sorted by bookingId.
List<String> getEnrolledStudents(String courseId)
  • Return the student IDs with confirmed bookings for this course, sorted by studentId.

Rules

  • All IDs are caller-supplied and case-sensitive. Sort IDs in ascending lexicographical order. Lists return [] when there are no matches or the requested student or course does not exist.
  • Failed operations change nothing and do not reserve IDs. Successful booking IDs remain used after cancellation. After cancelling a booking, a student may book the same course again using a new booking ID, subject to course capacity and the student's confirmed-booking limit.
  • Operations must be thread-safe and atomic. Concurrent requests must preserve course capacities, student limits, and the rule of one confirmed booking per student per course.
  • Payments, authentication, prerequisites, scheduling conflicts, and waitlists are outside the scope.

Constraints

  • 1 ≤ N ≤ 1,000; 1 ≤ capacity ≤ 10,000.
  • At most 10,000 method calls per system, excluding the constructor.
  • IDs contain 1 to 30 ASCII letters or digits.
  • Names, titles, and instructor names contain 1 to 100 ASCII letters, digits, or spaces, with at least one non-space character. Keyword length is 0 to 100 using the same characters.
  • All parameters satisfy these constraints. No parameter is null. Returned fields use commas without added spaces.

Examples

Calls below use one system in the shown order. Parameter names are labels for readability.

Example 1: Booking Limits

CourseBookingSystem(N = 2) creates the system.

  1. registerStudent(studentId = "R7", name = "Riya") → true
  2. registerStudent(studentId = "R3", name = "Dev") → true
  3. addCourse(courseId = "C8", title = "Data Modeling", instructorName = "Vikram", capacity = 1) → true
  4. addCourse(courseId = "C3", title = "Data Basics", instructorName = "Leena", capacity = 2) → true
  5. addCourse(courseId = "C5", title = "Networks", instructorName = "Omar", capacity = 1) → true
  6. bookCourse(bookingId = "K8", studentId = "R7", courseId = "C8") → "BOOKED"
  7. bookCourse(bookingId = "K3", studentId = "R7", courseId = "C3") → "BOOKED"
  8. bookCourse(bookingId = "K5", studentId = "R7", courseId = "C5") → "COURSE_LIMIT_REACHED"
  9. bookCourse(bookingId = "K6", studentId = "R7", courseId = "C3") → "ALREADY_BOOKED"
  10. bookCourse(bookingId = "K7", studentId = "R3", courseId = "C8") → "COURSE_FULL"
  11. bookCourse(bookingId = "K4", studentId = "R3", courseId = "C3") → "BOOKED"

R7 reaches the two-course limit. A duplicate booking is rejected before checking that limit. Course C8 has only one seat.

Example 2: Cancellation and Queries

Continue with the same system.

  1. cancelBooking(studentId = "R3", bookingId = "K8") → "NOT_BOOKING_OWNER"
  2. cancelBooking(studentId = "R7", bookingId = "K8") → "CANCELLED"
  3. cancelBooking(studentId = "R7", bookingId = "K8") → "ALREADY_CANCELLED"
  4. bookCourse(bookingId = "K5", studentId = "R7", courseId = "C5") → "BOOKED"
  5. bookCourse(bookingId = "K7", studentId = "R3", courseId = "C8") → "BOOKED"
  6. bookCourse(bookingId = "K8", studentId = "R7", courseId = "C8") → "BOOKING_ID_EXISTS"
  7. searchCourses(keyword = "DATA") → ["C3,Data Basics,Leena,2,0", "C8,Data Modeling,Vikram,1,0"]
  8. getStudentBookings(studentId = "R7") → ["K3,R7,C3,CONFIRMED", "K5,R7,C5,CONFIRMED", "K8,R7,C8,CANCELLED"]
  9. getEnrolledStudents(courseId = "C3") → ["R3", "R7"]

Cancelling K8 frees a seat and one booking allowance for R7. The previously rejected IDs K5 and K7 can be retried; the cancelled booking ID K8 remains used.



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