Status of Flight Tickets - Problem
Flight Ticket Status Tracker
You're building a flight reservation system that needs to track passenger ticket statuses in real-time. When passengers book tickets, they get confirmed if seats are available, or placed on a waitlist if the flight is full.
The Challenge: Given flight capacities and passenger booking times, determine each passenger's current ticket status.
Key Rules:
- Passengers are processed in chronological order of booking time
- If seats available โ ticket
Confirmed - If flight full โ passenger goes on
Waitlist - Each flight has a fixed capacity limit
Input: Two tables - Flights (flight_id, capacity) and Passengers (passenger_id, flight_id, booking_time)
Output: Each passenger's status ordered by passenger_id
Input & Output
example_1.py โ Basic Flight Booking
$
Input:
Flights: [[1, 2]], Passengers: [[1, 1, '2023-07-10 16:30:00'], [2, 1, '2023-07-10 17:00:00'], [3, 1, '2023-07-10 18:00:00']]
โบ
Output:
[[1, 'Confirmed'], [2, 'Confirmed'], [3, 'Waitlist']]
๐ก Note:
Flight 1 has capacity 2. Passengers 1 and 2 book first (confirmed), passenger 3 books last (waitlisted)
example_2.py โ Multiple Flights
$
Input:
Flights: [[1, 1], [2, 2]], Passengers: [[1, 1, '2023-07-10 16:00:00'], [2, 2, '2023-07-10 17:00:00'], [3, 1, '2023-07-10 18:00:00'], [4, 2, '2023-07-10 19:00:00']]
โบ
Output:
[[1, 'Confirmed'], [2, 'Confirmed'], [3, 'Waitlist'], [4, 'Confirmed']]
๐ก Note:
Flight 1 (capacity 1): P1 confirmed, P3 waitlisted. Flight 2 (capacity 2): P2 and P4 both confirmed
example_3.py โ Same Booking Time Edge Case
$
Input:
Flights: [[1, 1]], Passengers: [[1, 1, '2023-07-10 16:00:00'], [2, 1, '2023-07-10 16:00:00']]
โบ
Output:
[[1, 'Confirmed'], [2, 'Waitlist']]
๐ก Note:
When booking times are identical, passenger with lower ID gets priority (deterministic ordering)
Visualization
Tap to expand
Understanding the Visualization
1
Group Bookings by Flight
Separate passengers by their destination flight
2
Sort by Booking Time
Within each flight, arrange passengers by when they booked
3
Assign Sequential Numbers
Give each passenger a number (1st, 2nd, 3rd, etc.) in their flight
4
Compare with Capacity
If passenger number โค flight capacity: Confirmed, otherwise Waitlist
Key Takeaway
๐ฏ Key Insight: By processing passengers in chronological order within each flight and comparing their position with flight capacity, we efficiently determine ticket status in a single pass per flight.
Time & Space Complexity
Time Complexity
O(n log n)
Sorting passengers by booking_time within each flight partition
โก Linearithmic
Space Complexity
O(n)
Temporary space for sorting and window function processing
โก Linearithmic Space
Constraints
- 1 โค flights.length โค 104
- 1 โค passengers.length โค 105
- 1 โค flight_id, passenger_id, capacity โค 109
- booking_time format: 'YYYY-MM-DD HH:MM:SS'
- All passenger_id values are distinct
- All booking_time values are distinct
- Each passenger books exactly one flight
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code