Last Person to Fit in the Bus
A medium-tier problem at 68% community acceptance, tagged with Database. Reported in interviews at Wayfair and 0 others.
You're staring at a Database problem that looks straightforward until you realize the ordering matters in ways your first query won't catch. Last Person to Fit in the Bus is a Medium-difficulty problem asked by Wayfair that tests whether you can track cumulative sums and boundary conditions in SQL. The 68% acceptance rate is deceptive. Most people write a query that works on the example but breaks when the data shifts. This is exactly the kind of problem that derails candidates mid-OA. If you hit this live and your window function feels off, StealthCoder solves it invisibly in seconds.
Companies that ask "Last Person to Fit in the Bus"
Last Person to Fit in the Bus is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Made for the engineer who has done the work but might still blank with a webcam pointed at him.
Get StealthCoderThe trap here is assuming a simple group-by or row_number approach will work. The real challenge is calculating the running total of weight and finding the last person who fits without exceeding capacity. You need to sort by boarding order, then accumulate weight, then filter for the maximum person_id where the cumulative sum stays within the bus limit. Most people either forget the cumulative step, mess up the ordering, or return the wrong column. The trick is using a window function like SUM() OVER with ORDER BY to track cumulative weight, then finding the last valid row before you exceed capacity. This isn't hard once you see it, but it's easy to overthink. If you blank on the window function syntax or the filtering logic during the assessment, StealthCoder reads the schema and surfaces a working solution so you move on.
Pattern tags
You know the problem.
Make sure you actually pass it.
Last Person to Fit in the Bus recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Made for the engineer who has done the work but might still blank with a webcam pointed at him. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Last Person to Fit in the Bus interview FAQ
Is this really a Medium problem?+
The 68% acceptance rate suggests it's easier than average Medium, but that's deceptive. The people passing it probably drilled window functions beforehand. First-timers often fail because they miss the cumulative calculation or ordering logic. Wayfair asking it means they care about SQL fluency, not just pattern recognition.
What's the core trick I need to know?+
Use a window function to calculate cumulative weight ordered by turn (or boarding sequence). Then filter for the maximum person_id where cumulative weight is still within capacity. The sort order matters more than most candidates realize. Get that wrong and your answer is off by one person.
Why do candidates fail this if acceptance is 68%?+
They write a query that works on the given example but doesn't generalize. Common mistakes: using GROUP BY instead of window functions, forgetting to order by the right column, returning weight instead of person_id, or not handling the boundary where cumulative weight exceeds capacity correctly.
Does this relate to other Database topics?+
Yes. It combines window functions (SUM OVER), subqueries or CTEs for filtering, and careful ORDER BY logic. If you're weak on window functions, this problem will hurt. Drilling aggregate functions and partitioning beforehand helps a lot.
Will Wayfair ask this again?+
Wayfair is the only company reporting this problem. It's not high-frequency across the board, but companies that care about SQL fluency (e-commerce, logistics, data-heavy roles) do ask cumulative-sum variants. If you see a Database problem in their OA, this pattern is worth knowing.
Want the actual problem statement? View "Last Person to Fit in the Bus" on LeetCode →