Description
Table: Queue
| Column Name | Type |
|---|---|
| person_id | int |
| person_name | varchar |
| weight | int |
| turn | int |
person_idcolumn contains unique values.- This table has the information about all people waiting for a bus.
- The
person_idand turn columns will contain all numbers from1ton,wherenis the number of rows in the table. turndetermines the order of which the people will board the bus, whereturn=1denotes the first person to board andturn=ndenotes the last person to board.weightis the weight of the person in kilograms.
There is a queue of people waiting to board a bus. However, the bus has a weight limit of 1000 kilograms, so there may be some people who cannot board.
Problem Statement
Write a solution to find the person_name of the last person that can fit on the bus without exceeding the weight
limit. The test cases are generated such that the first person does not exceed the weight limit.
Note that only one person can board the bus at any given turn.
The result format is in the following example.
Example 1:
Input:
Queuetable:
| 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 |
Explanation:
The folowing table is ordered by the turn for simplicity.
| Turn | ID | Name | Weight | Total Weight | Note |
|---|---|---|---|---|---|
| 1 | 5 | Alice | 250 | 250 | |
| 2 | 3 | Alex | 350 | 600 | |
| 3 | 6 | John Cena | 400 | 1000 | (last person to board) |
| 4 | 2 | Marie | 200 | 1200 | (cannot board) |
| 5 | 4 | Bob | 175 | ___ | |
| 6 | 1 | Winston | 500 | ___ |
Solution
The problem essentially requires you to find the running sum of weight then you find the person with last turn which you can do if you order the results in descending order of total_weight WHERE total_weight <= 1000.
Let’s see how you can find the total_weight. You can find running total weight by using window function where you don’t partition by any field but rather order the rows by turn.
1SELECT person_id, person_name, weight, turn,
2 SUM(weight) OVER (ORDER BY turn) AS total_weight
3 FROM Queue;
This returns below results.
| person_id | person_name | weight | turn | total_weight |
|---|---|---|---|---|
| 5 | Alice | 250 | 1 | 250 |
| 3 | Alex | 350 | 2 | 600 |
| 6 | John Cena | 400 | 3 | 1000 |
| 2 | Marie | 200 | 4 | 1200 |
| 4 | Bob | 175 | 5 | 1375 |
| 1 | Winston | 500 | 6 | 1875 |
The next step is to filter the records where total_weight is less than or equal to 1000. You can use WHERE total_weight <= 1000 clause. When you do this, you will get only the first three rows from above table. To find the last person to fit in the bus, you can order the result by total_weight DESC LIMIT 1 clause and print only person_name column.
Below is the final solution. Notice that I have calculated total_weight in CTE clause and removed unnecessary columns.
1WITH running_weight AS (
2 SELECT person_name,
3 SUM(weight) OVER (ORDER BY turn) AS total_weight
4 FROM Queue
5) SELECT person_name
6 FROM running_weight
7 WHERE total_weight <= 1000
8 ORDER BY total_weight DESC
9 LIMIT 1;


Comments