Problem Description
Given two tables, Flights and Passengers, where each flight has a fixed capacity and passengers book tickets with a specific booking_time, determine whether each passenger’s ticket is confirmed or waitlisted. A ticket is confirmed if the number of bookings made before (or at the same time as) the current booking (when ordered by booking_time) is within the flight’s capacity. Otherwise, the ticket is marked as waitlisted. The final output should list passenger_id and ticket status (Confirmed/Waitlist) ordered by passenger_id in ascending order.
Key Insights
- For each flight, sort the associated passengers by their booking_time.
- The first "capacity" number of bookings on each flight get confirmed.
- Bookings beyond the flight's capacity are marked as waitlist.
- The final result must be sorted by passenger_id in ascending order.
- A join between Flights and Passengers (or keyed mapping in code) is required by flight_id.
Space and Time Complexity
Time Complexity: O(P log P) where P is the number of passengers (sorting the passengers per flight can contribute to the log factor).
Space Complexity: O(P + F) where P is the number of passengers and F is the number of flights.
Solution
The solution involves the following steps:
- Map each flight_id to its capacity.
- Group all passengers by flight_id and sort these groups by booking_time.
- For each flight group, iterate over the sorted passengers:
- Mark the first "capacity" passengers as Confirmed.
- Mark the rest as Waitlist.
- Store the results in a dictionary (or list) and finally output the results sorted by passenger_id.
Data structures used:
- Dictionary (hash map) to map flight_id to capacity.
- Dictionary to map flight_id to list of passengers.
- Sorting for ordering passengers by booking_time within each flight.
This algorithm ensures that we process each passenger only once (after grouping) and the result is then assembled in the required order.