{ "data": { "question": { "questionId": "1327", "questionFrontendId": "1204", "boundTopicId": null, "title": "Last Person to Fit in the Bus", "titleSlug": "last-person-to-fit-in-the-bus", "content": "

Table: Queue

\n\n
\n+-------------+---------+\n| Column Name | Type    |\n+-------------+---------+\n| person_id   | int     |\n| person_name | varchar |\n| weight      | int     |\n| turn        | int     |\n+-------------+---------+\nperson_id column contains unique values.\nThis table has the information about all people waiting for a bus.\nThe person_id and turn columns will contain all numbers from 1 to n, where n is the number of rows in the table.\nturn 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.\nweight is the weight of the person in kilograms.\n
\n\n

 

\n\n

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.

\n\n

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.

\n\n

The result format is in the following example.

\n\n

 

\n

Example 1:

\n\n
\nInput: \nQueue table:\n+-----------+-------------+--------+------+\n| person_id | person_name | weight | turn |\n+-----------+-------------+--------+------+\n| 5         | Alice       | 250    | 1    |\n| 4         | Bob         | 175    | 5    |\n| 3         | Alex        | 350    | 2    |\n| 6         | John Cena   | 400    | 3    |\n| 1         | Winston     | 500    | 6    |\n| 2         | Marie       | 200    | 4    |\n+-----------+-------------+--------+------+\nOutput: \n+-------------+\n| person_name |\n+-------------+\n| John Cena   |\n+-------------+\nExplanation: The folowing table is ordered by the turn for simplicity.\n+------+----+-----------+--------+--------------+\n| Turn | ID | Name      | Weight | Total Weight |\n+------+----+-----------+--------+--------------+\n| 1    | 5  | Alice     | 250    | 250          |\n| 2    | 3  | Alex      | 350    | 600          |\n| 3    | 6  | John Cena | 400    | 1000         | (last person to board)\n| 4    | 2  | Marie     | 200    | 1200         | (cannot board)\n| 5    | 4  | Bob       | 175    | ___          |\n| 6    | 1  | Winston   | 500    | ___          |\n+------+----+-----------+--------+--------------+\n
\n", "translatedTitle": null, "translatedContent": null, "isPaidOnly": false, "difficulty": "Medium", "likes": 565, "dislikes": 25, "isLiked": null, "similarQuestions": "[{\"title\": \"Running Total for Different Genders\", \"titleSlug\": \"running-total-for-different-genders\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"The Number of Seniors and Juniors to Join the Company\", \"titleSlug\": \"the-number-of-seniors-and-juniors-to-join-the-company\", \"difficulty\": \"Hard\", \"translatedTitle\": null}, {\"title\": \"The Number of Seniors and Juniors to Join the Company II\", \"titleSlug\": \"the-number-of-seniors-and-juniors-to-join-the-company-ii\", \"difficulty\": \"Hard\", \"translatedTitle\": null}]", "exampleTestcases": "{\"headers\":{\"Queue\":[\"person_id\",\"person_name\",\"weight\",\"turn\"]},\"rows\":{\"Queue\":[[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]]}}", "categoryTitle": "Database", "contributors": [], "topicTags": [ { "name": "Database", "slug": "database", "translatedName": null, "__typename": "TopicTagNode" } ], "companyTagStats": null, "codeSnippets": [ { "lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below\n", "__typename": "CodeSnippetNode" }, { "lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */\n", "__typename": "CodeSnippetNode" }, { "lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */\n", "__typename": "CodeSnippetNode" }, { "lang": "Pandas", "langSlug": "pythondata", "code": "import pandas as pd\n\ndef last_passenger(queue: pd.DataFrame) -> pd.DataFrame:\n ", "__typename": "CodeSnippetNode" }, { "lang": "PostgreSQL", "langSlug": "postgresql", "code": "-- Write your PostgreSQL query statement below\n", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"72.6K\", \"totalSubmission\": \"109.8K\", \"totalAcceptedRaw\": 72648, \"totalSubmissionRaw\": 109768, \"acRate\": \"66.2%\"}", "hints": [], "solution": null, "status": null, "sampleTestCase": "{\"headers\":{\"Queue\":[\"person_id\",\"person_name\",\"weight\",\"turn\"]},\"rows\":{\"Queue\":[[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]]}}", "metaData": "{\"manual\": false, \"mysql\": [\"Create table If Not Exists Queue (person_id int, person_name varchar(30), weight int, turn int)\"], \"mssql\": [\"Create table Queue (person_id int, person_name varchar(30), weight int, turn int)\"], \"oraclesql\": [\"Create table Queue (person_id int, person_name varchar(30), weight int, turn int)\"], \"database\": true, \"name\": \"last_passenger\", \"pythondata\": [\"Queue = pd.DataFrame([], columns=['person_id', 'person_name', 'weight', 'turn']).astype({'person_id':'Int64', 'person_name':'object', 'weight':'Int64', 'turn':'Int64'})\"], \"postgresql\": [\"\\nCreate table If Not Exists Queue (person_id int, person_name varchar(30), weight int, turn int)\"], \"database_schema\": {\"Queue\": {\"person_id\": \"INT\", \"person_name\": \"VARCHAR(30)\", \"weight\": \"INT\", \"turn\": \"INT\"}}}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [ "Create table If Not Exists Queue (person_id int, person_name varchar(30), weight int, turn int)", "Truncate table Queue", "insert into Queue (person_id, person_name, weight, turn) values ('5', 'Alice', '250', '1')", "insert into Queue (person_id, person_name, weight, turn) values ('4', 'Bob', '175', '5')", "insert into Queue (person_id, person_name, weight, turn) values ('3', 'Alex', '350', '2')", "insert into Queue (person_id, person_name, weight, turn) values ('6', 'John Cena', '400', '3')", "insert into Queue (person_id, person_name, weight, turn) values ('1', 'Winston', '500', '6')", "insert into Queue (person_id, person_name, weight, turn) values ('2', 'Marie', '200', '4')" ], "enableRunCode": true, "enableTestMode": false, "enableDebugger": false, "envInfo": "{\"mysql\": [\"MySQL\", \"

MySQL 8.0.

\"], \"mssql\": [\"MS SQL Server\", \"

mssql server 2019.

\"], \"oraclesql\": [\"Oracle\", \"

Oracle Sql 11.2.

\"], \"pythondata\": [\"Pandas\", \"

Python 3.10 with Pandas 2.0.2 and NumPy 1.25.0

\"], \"postgresql\": [\"PostgreSQL\", \"

PostgreSQL 16

\"]}", "libraryUrl": null, "adminUrl": null, "challengeQuestion": null, "__typename": "QuestionNode" } } }