Boats to Save People solution explained
Boats to Save People has a short greedy answer: sort the weights, put one pointer on the lightest person and one on the heaviest, and launch a boat for the heaviest each round, adding the lightest only if the pair fits under the limit. The heaviest person needs a boat no matter what, and the lightest is the partner most likely to fit, so the method is optimal at O(n log n) time.
By Evin JohnPublished 7 min read
The problem in plain words
A group of hikers needs to cross a lake. Every canoe takes at most two people, and each canoe has the same weight limit. You know every hiker's weight, and nobody is heavier than the limit on their own. What is the smallest number of canoes that gets the whole group across? Each canoe makes one trip, so this is purely about grouping people into pairs and singles.
| Weights | Limit | Boats | Grouping |
|---|---|---|---|
| [3, 5, 3, 4] | 6 | 3 | (3, 3), (4), (5) |
| [1, 2] | 3 | 1 | Both fit together exactly at the limit |
| [4, 4, 4] | 5 | 3 | No two people fit together |
| [2, 7, 1, 6, 5] | 8 | 3 | (7, 1), (6, 2), (5) |
| [6] | 6 | 1 | One person, one boat |
Clarify three things: is the cap really two people per boat (if it were larger, the problem becomes much harder), is a pair allowed to weigh exactly the limit (normally yes), and is every person at or under the limit by themselves (the usual guarantee, and needed for an answer to exist)?
Where this shows up and what it tests
This is a common medium-level greedy, often used in phone screens because the code is short and the reasoning is where candidates separate. The interviewer wants three things: that you sort, that you pick a pairing rule and can say why it is safe, and that you handle the pointer movement without an off-by-one when only one person is left.
Why the heaviest person decides each boat
Look at the heaviest person still on shore. They must ride in some boat, and their only possible partners are people light enough to fit with them. If even the lightest remaining person does not fit, the heaviest goes alone in every valid plan, so sending them alone loses nothing. If the lightest does fit, pairing them is safe: in any optimal plan, swap the heaviest person's partner (or empty seat) with the lightest person. The lightest weighs no more than whoever they replace, so every boat stays under the limit, and the boat count is unchanged.
This is the opposite of pairing the two lightest people first. That rule can waste a light person on another light person, leaving several heavy people who each need their own boat.
Slow approach: best fit for the heaviest
A natural first idea is a best-fit rule: take the heaviest remaining person, then search for the heaviest partner who still fits, so you use as much of the boat as possible. It also gives the optimal count, but finding and removing that partner from a list costs O(n) per boat, so the total is O(n²).
def boats_slow(weights, limit):
shore = sorted(weights)
trips = 0
while shore:
heavy = shore.pop() # heaviest person leaves on this boat
for j in range(len(shore) - 1, -1, -1):
if heavy + shore[j] <= limit:
shore.pop(j) # heaviest partner who still fits
break
trips += 1
return tripsMain approach: two pointers from both ends
Sort the weights. Set light to 0 and heavy to the last index. While light is at most heavy, one boat leaves: if weights[light] + weights[heavy] fits, the light person goes too, so move light up by one. Either way, move heavy down by one and count the boat. When light and heavy meet on the same person, that person takes the last boat alone, and the sum check is harmless because the loop moves heavy past light right away.
def boats(weights, limit):
w = sorted(weights)
light, heavy = 0, len(w) - 1
trips = 0
while light <= heavy:
if w[light] + w[heavy] <= limit:
light += 1 # the lightest rides along
heavy -= 1 # the heaviest always leaves
trips += 1
return tripsfunction boats(weights, limit) {
const w = [...weights].sort((a, b) => a - b);
let light = 0, heavy = w.length - 1, trips = 0;
while (light <= heavy) {
if (w[light] + w[heavy] <= limit) light++;
heavy--;
trips++;
}
return trips;
}When the weights are small integers, a counting sort replaces the comparison sort and the whole solution becomes O(n + limit). That is worth mentioning if the interviewer asks whether you can beat O(n log n).
Tracing the fourth example
Weights [2, 7, 1, 6, 5] with a limit of 8 sort to [1, 2, 5, 6, 7]. Round one: 1 + 7 = 8 fits, so both go; light moves to 2, heavy to 6, one boat. Round two: 2 + 6 = 8 fits; light moves to 5, heavy also points at 5, two boats. Round three: light and heavy are both the person weighing 5, who leaves alone, three boats. The pointers cross and the loop ends.
Complexity
| Approach | Time | Extra space |
|---|---|---|
| Try every pairing | Exponential | O(n) |
| Best fit with list removal | O(n²) | O(n) |
| Sort plus two pointers | O(n log n) | O(n) for a sorted copy |
| Counting sort plus two pointers | O(n + limit) | O(limit) |
Edge cases to test
- One person: exactly one boat.
- Everyone weighs exactly the limit: one boat each.
- Everyone weighs at most half the limit: every two people share, so the answer is the count divided by two, rounded up.
- A pair that adds up to exactly the limit: it must be allowed, so the check is less than or equal.
- An odd number of people, where the middle person ends up alone.
- Duplicate weights, which the sort and pointers handle without special code.
Mistakes that cost points
- Using while light < heavy, which forgets to send the final person when the pointers meet.
- Moving the light pointer every round, even when the pair does not fit, which strands light people.
- Pairing the two lightest people first, which wastes small weights and inflates the count.
- In JavaScript, calling sort() with no comparator, so 10 sorts before 9.
- Allowing three people in a boat because the weight fits. The seat cap is two.
- Counting boats only when a pair forms, so solo trips go missing.
How to explain it out loud
Fictional example
- Candidate
- I sort the weights. The heaviest person must leave on some boat, so each round I launch a boat for them.
- Candidate
- If the lightest person fits with them, I add the lightest. Swapping anyone else out for the lightest can only make a boat lighter, so this never costs an extra trip.
- Candidate
- Two pointers walk inward, n steps in total, so the sort dominates at O(n log n). Ending the loop at light less than or equal to heavy makes sure the last person is counted.
Other problems in this set
Bag of Tokens uses the same two-pointer layout on a sorted list, but each end means something different: cheap tokens buy points, expensive tokens buy power. For another sorted greedy with a short exchange-argument proof, look at Two City Scheduling. The data structure interview questions page covers arrays, sorting and pointer techniques in general.
Practice it with Natively
Natively fits into a practice routine for two-pointer problems like this one. It can read the prompt from a screenshot and propose a pairing rule with a one-line reason, which you can test against your own idea once you have made an attempt. It also transcribes a mock round on your computer, so you can play back whether your loop condition explanation was clear. Details on an AI interview assistant for Mac and Windows.
Questions people ask
How do you solve Boats to Save People?
Sort the weights and use two pointers. Each round the heaviest remaining person takes a boat, and the lightest remaining person joins if their combined weight is within the limit. Count one boat per round until the pointers cross.
Why pair the heaviest person with the lightest?
The heaviest person needs a boat in every plan, and the lightest person is the partner most likely to fit. Swapping any other partner for the lightest keeps every boat within the limit, so the count never gets worse.
What is the time complexity?
O(n log n) for sorting and O(n) for the two-pointer pass. With small integer weights, a counting sort makes it O(n + limit).
Why is the loop condition light <= heavy and not light < heavy?
When both pointers land on the same person, that person still needs a boat. A strict less-than would skip them and undercount by one.
Where to go next
Practice with the same assistant you would use on a call: it listens, transcribes and suggests answers from your own resume and the job description.
Facts on this page were last checked on . Product details come from the Natively app itself; if something here is out of date, tell us at hello@heynatively.com.
