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
Flight Ticket Status Determinationโœˆ๏ธ Flight 101Capacity: 2 seats๐Ÿ‘ค P1: 09:00 โ†’ Rank 1๐Ÿ‘ค P3: 09:30 โ†’ Rank 2๐Ÿ‘ค P5: 10:00 โ†’ Rank 3โœˆ๏ธ Flight 102Capacity: 1 seat๐Ÿ‘ค P2: 08:45 โ†’ Rank 1๐Ÿ‘ค P4: 09:15 โ†’ Rank 2๐Ÿ“‹ Final ResultsP1: โœ… ConfirmedP2: โœ… ConfirmedP3: โœ… ConfirmedP4: โณ WaitlistP5: โณ Waitlist๐Ÿ” Algorithm Process1๏ธโƒฃ Group passengers by flight_id2๏ธโƒฃ Sort each group by booking_time (earliest first)3๏ธโƒฃ Assign rank numbers (ROW_NUMBER): 1st, 2nd, 3rd...4๏ธโƒฃ If rank โ‰ค capacity โ†’ Confirmed, else โ†’ Waitlistโšก Time Complexity: O(n log n) | Space: O(n)
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

n
2n
โšก Linearithmic
Space Complexity
O(n)

Temporary space for sorting and window function processing

n
2n
โšก 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
Asked in
Expedia 45 Booking.com 38 Airbnb 32 American Airlines 28
43.7K Views
High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen