Last Person to Fit in the Bus - Problem

You are given a table Queue representing people waiting to board a bus. The bus has a weight limit of 1000 kilograms.

Each person has:

  • person_id - unique identifier
  • person_name - name of the person
  • weight - weight in kilograms
  • turn - boarding order (1 = first to board)

Write a SQL query to find the name of the last person who can board the bus without exceeding the weight limit of 1000kg.

Note: People board in order of their turn value, and the first person is guaranteed to fit on the bus.

Table Schema

Queue
Column Name Type Description
person_id PK int Unique identifier for each person
person_name varchar Name of the person
weight int Weight of person in kilograms
turn int Boarding order (1 = first, n = last)
Primary Key: person_id
Note: Turn values range from 1 to n where n is total number of people

Input & Output

Example 1 — Basic Bus Loading
Input 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:

When ordered by turn: Alice (250) → Alex (350) → John Cena (400) → Marie (200) → Bob (175) → Winston (500). Cumulative weights: 250 → 600 → 1000 → 1200. John Cena is the last person whose cumulative weight (1000kg) doesn't exceed the limit.

Example 2 — Early Weight Limit
Input Table:
person_id person_name weight turn
1 Alice 800 1
2 Bob 175 2
3 Charlie 80 3
Output:
person_name
Bob
💡 Note:

Cumulative weights in order: Alice (800) → Bob (975) → Charlie (1055). Since Charlie would exceed 1000kg, Bob is the last person who can board.

Constraints

  • 1 ≤ n ≤ 1000 where n is the number of people
  • 1 ≤ person_id ≤ n
  • 1 ≤ weight ≤ 1000
  • 1 ≤ turn ≤ n
  • All person_id and turn values are unique
  • The first person (turn=1) will not exceed the weight limit

Visualization

Tap to expand
Last Person to Fit in the Bus INPUT ID Name Weight Turn 1 Alice 250 1 2 Bob 300 2 3 Carol 400 3 4 Dan 150 4 Limit: 1000 kg People queue by turn order Total: 1100 kg (exceeds!) 250+300+400+150 = 1100 ALGORITHM STEPS 1 Sort by Turn ORDER BY turn ASC 2 Running Sum SUM(weight) OVER 3 Filter Valid WHERE total <= 1000 4 Get Last ORDER BY turn DESC LIMIT 1 Turn Name CumSum Valid 1 Alice 250 OK 2 Bob 550 OK 3 Carol 950 OK 4 Dan 1100 NO --> Last valid: Carol (950 <= 1000) Dan excluded (1100 > 1000) 950 + 150 = 1100 exceeds limit FINAL RESULT BUS - 950/1000 kg Alice Bob Carol Dan Cannot board ANSWER: Carol Last person to board without exceeding limit Cumulative: 950 kg Key Insight: Use window functions to calculate cumulative weight as people board in turn order. Filter rows where cumulative sum <= 1000, then select the last valid person (highest turn). SQL: SUM(weight) OVER (ORDER BY turn) calculates running total efficiently in O(n) time. TutorialsPoint - Last Person to Fit in the Bus | Optimal Solution
Asked in
Amazon 15 Microsoft 12 Google 8
28.5K Views
Medium Frequency
~12 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