Boats to Save People
MediumTwo Pointerstwo pointers
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
people[i] is the weight of person i. Every boat can carry **at most two people** at once, provided their combined weight is at most limit. No single person weighs more than limit.
Return the minimum number of boats required to carry everyone.
An O(n log n) solution is expected.
Examples
Input: people = [1,2], limit = 3
Output: 1
Both people fit in one boat (1 + 2 = 3).
Input: people = [3,2,2,1], limit = 3
Output: 3
Boats: (1,2), (2), (3).
Input: people = [3,5,3,4], limit = 5
Output: 4
No pair fits under 5, so everyone rides alone.
Constraints
- 1 <= people.length <= 5 * 10^4
- 1 <= people[i] <= limit <= 3 * 10^4
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.