Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using core interview patterns strategy.
Table: Queue
+-------------+---------+ | 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.
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.
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: 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 | +-------------+ Explanation: The folowing table is ordered by the turn for simplicity. +------+----+-----------+--------+--------------+ | Turn | ID | Name | Weight | Total Weight | +------+----+-----------+--------+--------------+ | 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 | ___ | +------+----+-----------+--------+--------------+
Problem summary: Table: Queue +-------------+---------+ | 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. 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. Write a solution to find the person_name of the last person that can fit on the bus without exceeding the weight limit. The
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
{"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]]}}running-total-for-different-genders)the-number-of-seniors-and-juniors-to-join-the-company)the-number-of-seniors-and-juniors-to-join-the-company-ii)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1204: Last Person to Fit in the Bus
// Auto-generated Java example from rust.
class Solution {
public void exampleSolution() {
}
}
// Reference (rust):
// // Accepted solution for LeetCode #1204: Last Person to Fit in the Bus
// pub fn sql_example() -> &'static str {
// r#"
// -- Accepted solution for LeetCode #1204: Last Person to Fit in the Bus
// # Write your MySQL query statement below
// SELECT a.person_name
// FROM
// Queue AS a,
// Queue AS b
// WHERE a.turn >= b.turn
// GROUP BY a.person_id
// HAVING SUM(b.weight) <= 1000
// ORDER BY a.turn DESC
// LIMIT 1;
// "#
// }
// Accepted solution for LeetCode #1204: Last Person to Fit in the Bus
// Auto-generated Go example from rust.
func exampleSolution() {
}
// Reference (rust):
// // Accepted solution for LeetCode #1204: Last Person to Fit in the Bus
// pub fn sql_example() -> &'static str {
// r#"
// -- Accepted solution for LeetCode #1204: Last Person to Fit in the Bus
// # Write your MySQL query statement below
// SELECT a.person_name
// FROM
// Queue AS a,
// Queue AS b
// WHERE a.turn >= b.turn
// GROUP BY a.person_id
// HAVING SUM(b.weight) <= 1000
// ORDER BY a.turn DESC
// LIMIT 1;
// "#
// }
# Accepted solution for LeetCode #1204: Last Person to Fit in the Bus
# Auto-generated Python example from rust.
def example_solution() -> None:
return
# Reference (rust):
# // Accepted solution for LeetCode #1204: Last Person to Fit in the Bus
# pub fn sql_example() -> &'static str {
# r#"
# -- Accepted solution for LeetCode #1204: Last Person to Fit in the Bus
# # Write your MySQL query statement below
# SELECT a.person_name
# FROM
# Queue AS a,
# Queue AS b
# WHERE a.turn >= b.turn
# GROUP BY a.person_id
# HAVING SUM(b.weight) <= 1000
# ORDER BY a.turn DESC
# LIMIT 1;
# "#
# }
// Accepted solution for LeetCode #1204: Last Person to Fit in the Bus
pub fn sql_example() -> &'static str {
r#"
-- Accepted solution for LeetCode #1204: Last Person to Fit in the Bus
# Write your MySQL query statement below
SELECT a.person_name
FROM
Queue AS a,
Queue AS b
WHERE a.turn >= b.turn
GROUP BY a.person_id
HAVING SUM(b.weight) <= 1000
ORDER BY a.turn DESC
LIMIT 1;
"#
}
// Accepted solution for LeetCode #1204: Last Person to Fit in the Bus
// Auto-generated TypeScript example from rust.
function exampleSolution(): void {
}
// Reference (rust):
// // Accepted solution for LeetCode #1204: Last Person to Fit in the Bus
// pub fn sql_example() -> &'static str {
// r#"
// -- Accepted solution for LeetCode #1204: Last Person to Fit in the Bus
// # Write your MySQL query statement below
// SELECT a.person_name
// FROM
// Queue AS a,
// Queue AS b
// WHERE a.turn >= b.turn
// GROUP BY a.person_id
// HAVING SUM(b.weight) <= 1000
// ORDER BY a.turn DESC
// LIMIT 1;
// "#
// }
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.