Find the Final Boarding Person before the Bus Limit Is Exceeded
Company: Amazon
Role: Software Engineer
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
### Problem
Return person_name for the last person who boards in turn order without exceeding the bus capacity of 1000 kilograms. Boarding follows the queue prefix and stops when the next person would exceed the limit; people later in the queue do not skip ahead. The source guarantees the first person fits.
### Tables
Table: `Queue`
```text
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| person_id | int |
| person_name | varchar |
| weight | int |
| turn | int |
+-------------+---------+
person_id column contains unique values.
This table has the information about all people waiting for a bus.
The person_id and turn columns will contain all numbers from 1 to n, where n is the number of rows in the table.
turn determines the order of which the people will board the bus, where turn=1 denotes the first person to board and turn=n denotes the last person to board.
weight is the weight of the person in kilograms.
```
### Constraints
- In this version, every `weight` is a known, non-NULL, nonnegative integer. No additional upper bound is imposed.
### Example
```text
Queue (person_id,person_name,weight,turn): (3,Ada,300,1), (1,Bo,450,2), (2,Cam,250,3), (4,Dee,1,4)
Output (person_name): (Cam)
Cumulative weights by turn: 300,750,1000,1001.
```
### What a Strong Answer Covers
- Keep the queue's boarding order and allow a total exactly equal to the capacity.
- Identify the last person who actually boards before the queue stops.
### Follow-up Questions
- How would you report the unused capacity?
- How would the result change if several identical buses were available?
Overview: Find the last person who can board in queue order without exceeding the bus weight limit.