Flight Occupancy and Waitlist Analysis - Problem

You're working for an airline reservation system that needs to analyze flight bookings and manage passenger seating efficiently. The system has two main components:

๐Ÿ“‹ Database Tables

Flights Table: Contains flight information with columns flight_id (unique identifier) and capacity (maximum number of seats).

Passengers Table: Contains passenger bookings with columns passenger_id (unique identifier) and flight_id (the flight they want to book).

๐ŸŽฏ The Challenge

When passengers book tickets, the airline follows a simple rule: first come, first served. If there are available seats when a passenger books, they get confirmed. If the flight is already at full capacity, they go on the waitlist.

Your task: Write a SQL query to calculate for each flight:

  • Number of passengers with confirmed seats
  • Number of passengers on the waitlist

Return results ordered by flight_id in ascending order.

Input & Output

example_1.sql โ€” Basic Scenario
$ Input: Flights table: +----------+----------+ | flight_id| capacity | +----------+----------+ | 1 | 4 | | 2 | 2 | | 3 | 6 | +----------+----------+ Passengers table: +--------------+----------+ | passenger_id | flight_id| +--------------+----------+ | 101 | 1 | | 102 | 1 | | 103 | 1 | | 104 | 2 | | 105 | 2 | | 106 | 2 | +--------------+----------+
โ€บ Output: +----------+------------+---------------+ | flight_id| booked_cnt | waitlist_cnt | +----------+------------+---------------+ | 1 | 3 | 0 | | 2 | 2 | 1 | | 3 | 0 | 0 | +----------+------------+---------------+
๐Ÿ’ก Note: Flight 1 has capacity 4 and 3 passengers, so all 3 get seats. Flight 2 has capacity 2 and 3 passengers, so 2 get seats and 1 is waitlisted. Flight 3 has no bookings.
example_2.sql โ€” Full Capacity
$ Input: Flights table: +----------+----------+ | flight_id| capacity | +----------+----------+ | 10 | 3 | | 20 | 1 | +----------+----------+ Passengers table: +--------------+----------+ | passenger_id | flight_id| +--------------+----------+ | 1 | 10 | | 2 | 10 | | 3 | 10 | | 4 | 10 | | 5 | 20 | +--------------+----------+
โ€บ Output: +----------+------------+---------------+ | flight_id| booked_cnt | waitlist_cnt | +----------+------------+---------------+ | 10 | 3 | 1 | | 20 | 1 | 0 | +----------+------------+---------------+
๐Ÿ’ก Note: Flight 10: passengers 1,2,3 get seats (first come first served by passenger_id), passenger 4 waitlisted. Flight 20: passenger 5 gets the only seat.
example_3.sql โ€” Edge Case: No Passengers
$ Input: Flights table: +----------+----------+ | flight_id| capacity | +----------+----------+ | 100 | 5 | | 200 | 3 | +----------+----------+ Passengers table: +--------------+----------+ | passenger_id | flight_id| +--------------+----------+ (empty table)
โ€บ Output: +----------+------------+---------------+ | flight_id| booked_cnt | waitlist_cnt | +----------+------------+---------------+ | 100 | 0 | 0 | | 200 | 0 | 0 | +----------+------------+---------------+
๐Ÿ’ก Note: Both flights have no passengers, so booked and waitlist counts are 0 for all flights.

Visualization

Tap to expand
โœˆ๏ธ Flight Reservation SystemFlight AA101 - Capacity: 4P1Seat 1P2Seat 2P3Seat 3P4Seat 4๐ŸŽซ All Seats TakenBooked: 4, Waitlist: 0Flight BB202 - Capacity: 2P5Seat 1P6Seat 2P7P8WaitlistBooked: 2, Waitlist: 2๐Ÿ” SQL Window Function MagicROW_NUMBER() OVER (PARTITION BY flight_id ORDER BY passenger_id)โ†’ Assigns booking sequence: 1, 2, 3, 4...SUM(CASE WHEN row_number <= capacity THEN 1 END) AS bookedSUM(CASE WHEN row_number > capacity THEN 1 END) AS waitlist๐ŸŸข Confirmed Booking ๐Ÿ”ด Waitlisted ๐ŸŽซ Full Capacity
Understanding the Visualization
1
Passenger Arrival
Passengers book flights in order (simulated by passenger_id sequence)
2
Seat Assignment
First N passengers get confirmed seats where N = flight capacity
3
Waitlist Management
Remaining passengers are placed on waitlist for potential cancellations
4
Report Generation
Count and report confirmed vs waitlisted passengers per flight
Key Takeaway
๐ŸŽฏ Key Insight: ROW_NUMBER() window function perfectly simulates first-come-first-served booking by assigning sequence numbers, then conditional aggregation efficiently splits passengers into confirmed vs waitlisted categories.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n)

ROW_NUMBER() requires sorting passengers within each flight partition

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

Space for window function results and grouping

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค flights.length โ‰ค 104
  • 1 โ‰ค passengers.length โ‰ค 105
  • 1 โ‰ค flight_id, passenger_id โ‰ค 106
  • 0 โ‰ค capacity โ‰ค 104
  • All flight_id values in Flights table are unique
  • All passenger_id values in Passengers table are unique
  • All passenger bookings reference valid flights
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28 Apple 22 Netflix 18
42.7K Views
Medium Frequency
~15 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