Last Person on Bus

4pts
Amazon

Problem Statement

Amazon logistics needs to calculate bus boarding limits. People board a bus in an order defined by their turn. The bus has a maximum capacity of 1000kg.

Write an SQL query to find the person_name of the last person who can board the bus without exceeding the 1000kg limit.

Rules:

  • Boarding order is strictly defined by the turn column.
  • You must find the person with the highest turn whose cumulative weight (including everyone before them) is less than or equal to 1000.
  • Return only the person_name.
Tests your understanding of
Basic SQL, Window Functions, Cumulative Sum and LIMIT

Input Tables

queue
person_id(INTEGER)person_name(VARCHAR)weight(INTEGER)turn(INTEGER)
5Alice2501
3Alex3502
6John4003
2Winston2004
7Marie2005
1Bob1756
4Thomas1007
8Sarah1508
9Mike509
10Jane30010
11Kevin10011
12Laura15012

Expected Output

person_name(VARCHAR)
John

Tags

MediumBasic SQLWindow FunctionsCumulative SumLIMIT
15-20 min
50%

Hints