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 personperson_name- the person's nameweight- their weight in kilogramsturn- 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
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)
โก Linearithmic
Space Complexity
O(n)
Space for storing the cumulative weight calculations
โก 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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code