Last Person to Fit in the Bus - Problem

Imagine you're managing a bus boarding system with a strict weight limit! You have a queue of people waiting to board a bus, but there's a catch - the bus can only carry a maximum of 1000 kilograms.

You're given a table Queue with information about each person:

  • person_id - unique identifier for each person
  • person_name - the person's name
  • weight - their weight in kilograms
  • turn - their position in the boarding queue (1 = first to board)

People board the bus one by one in order of their turn number. Your task is to find the name of the last person who can successfully board the bus without exceeding the 1000kg weight limit.

Goal: Write a SQL query to determine who gets the last available seat before the bus reaches its weight capacity!

Input & Output

example_1.sql โ€” Basic Queue
$ Input: Queue table: | person_id | person_name | weight | turn | |-----------|-------------|--------|------| | 5 | Alice | 250 | 1 | | 4 | Bob | 175 | 5 | | 3 | Alex | 350 | 2 | | 6 | John Cena | 400 | 3 | | 1 | Winston | 500 | 6 | | 2 | Marie | 200 | 4 |
โ€บ Output: | person_name | |-------------| | John Cena |
๐Ÿ’ก Note: People board in turn order: Alice(250) โ†’ Alex(350) โ†’ John Cena(400) โ†’ Marie(200) โ†’ Bob(175) โ†’ Winston(500). Cumulative weights: Alice=250, Alex=600, John Cena=1000, Marie=1200. John Cena is the last person who can board without exceeding 1000kg limit.
example_2.sql โ€” Everyone Fits
$ Input: Queue table: | person_id | person_name | weight | turn | |-----------|-------------|--------|------| | 1 | Tom | 200 | 1 | | 2 | Jerry | 150 | 2 | | 3 | Spike | 300 | 3 |
โ€บ Output: | person_name | |-------------| | Spike |
๐Ÿ’ก Note: All people can fit: Tom(200) + Jerry(150) + Spike(300) = 650kg โ‰ค 1000kg. Spike is the last person in the queue, so he's the answer.
example_3.sql โ€” Only First Person Fits
$ Input: Queue table: | person_id | person_name | weight | turn | |-----------|-------------|--------|------| | 1 | Heavy | 900 | 1 | | 2 | Light | 200 | 2 |
โ€บ Output: | person_name | |-------------| | Heavy |
๐Ÿ’ก Note: Heavy boards first with 900kg โ‰ค 1000kg โœ“. Light would make total 1100kg > 1000kg โœ—. So Heavy is the last (and only) person who can board.

Visualization

Tap to expand
๐ŸšŒ Bus Boarding Weight Management SystemWeight: 650kg / 1000kgโœ“ Within LimitAlice250kgTurn 1โœ“ BoardedAlex350kgTurn 2โœ“ BoardedJohn Cena400kgTurn 3LAST TO FITMarie200kgTurn 4โœ— Too heavyโœ—Cumulative Weight TrackingAlice: 250kg+Alex: 600kg+John: 1000kg โœ“+Marie: 1200kg โœ—Running Total Calculation (Window Function)SUM(weight) OVER(ORDER BY turn) keeps track of cumulative weight๐ŸŽฏ Result: John Cena is the last person who can board the bus!
Understanding the Visualization
1
Queue Formation
People line up in order of their turn numbers (1, 2, 3, ...)
2
Sequential Boarding
Each person boards and adds their weight to the running total
3
Weight Monitoring
Keep track of cumulative weight after each person boards
4
Limit Detection
Stop when next person would exceed 1000kg limit
5
Result
The last person who successfully boarded is our answer
Key Takeaway
๐ŸŽฏ Key Insight: Use window functions to calculate running totals efficiently, then find the last valid person before exceeding the weight limit. This transforms a potentially complex problem into a simple cumulative sum with filtering.

Time & Space Complexity

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

Sorting by turn takes O(n log n), window function runs in O(n)

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

Space for storing the cumulative weight calculations

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค person_id โ‰ค 1000
  • 1 โ‰ค turn โ‰ค 1000
  • 1 โ‰ค weight โ‰ค 1000
  • All person_id values are unique
  • All turn values are unique and form sequence 1 to n
  • The first person in queue (turn=1) will always fit within 1000kg limit
  • person_name contains only letters and spaces, max length 100
Asked in
Amazon 45 Microsoft 32 Google 28 Meta 15
28.5K Views
Medium Frequency
~15 min Avg. Time
892 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