Biggest tank
medium
New here? How this page works
- Read the Problem tab. Read it twice. Look at the examples: what goes in, what should come out.
- Open Break it down. Answer the questions in your own words. This is your plan.
- Write code on the right. Start small. Stuck on a blank page? Press “Write my plan as comments”.
- Press Run examples. See which examples pass. Red ones show what your code gave and what was expected.
- Stuck? Use Hints, one at a time. Still stuck after all of them? Read the Walkthrough. That's learning, not cheating.
- Press Submit. It runs hidden tests too. Pass them all, and it's solved.
A row of walls stands 1 metre apart. heights[i] is the height of the wall at position i.
Pick any two walls. Together with the ground between them, they make a tank. The water can only rise as high as the shorter of the two walls, so the tank holds (distance between the walls) × (height of the shorter wall) units of water. Walls in between don't get in the way.
Return the most water any tank can hold.
Examples
Example 1
- Input
- heights = [2, 7, 4, 1, 6, 3, 7, 5]
- Output
- 35
The walls at positions 1 and 6 are both 7 high and 5 apart: 5 × 7 = 35.
Example 2
- Input
- heights = [3, 3]
- Output
- 3
1 apart, both 3 high: 1 × 3 = 3.
Example 3
- Input
- heights = [4, 1, 1, 1, 4]
- Output
- 16
The two 4s are 4 apart: 4 × 4 = 16. The short walls between them don't matter.
Rules
heightshas between 2 and 100,000 walls.- Heights are whole numbers from 0 to 100,000.
Words on this page, in plain English (1)Open if any word is new to you
- Index (position)
- Where an item sits in a list. Counting starts at 0, not 1, so the first item is at position 0.
Answer each question in your own words before you look at ours. Rough notes are fine: the point is to think before you type code. Your answers stay in this browser.
1.What goes in?
Name every input and what it looks like. Can it be empty? Sorted? Negative?
Stuck? Each hint gives a little more away. Try the problem again after each one.
This explains the whole solution.
You will learn the most by trying first, even if you don’t finish. But if this pattern is new to you, reading the walkthrough first is a fine way to learn it.
1. Understand the problem
Any two walls make a tank. Its water is width × height, where the width is how far apart the walls are, and the height is the shorter wall (water spills over the short side). Walls in between are ignored. We want the biggest tank.
2. Solve an example by hand
[2, 7, 4, 1, 6, 3, 7, 5]. A wide tank is good, but its walls need to be tall too. The first and last walls are 7 apart, but the first is only 2 high: 7 × 2 = 14. The two walls of height 7, at positions 1 and 6, are 5 apart: 5 × 7 = 35. That turns out to be the best.
3. The slow way
Try every pair:
function biggestTank(heights) { let best = 0; for (let i = 0; i < heights.length; i++) { for (let j = i + 1; j < heights.length; j++) { best = Math.max(best, (j - i) * Math.min(heights[i], heights[j])); } } return best;}Correct, but O(n²): about 5 billion pairs for 100,000 walls. Far too slow.
4. Spot the pattern
Pairs from a list, where we want to skip pairs that can't win: think two pointers from both ends. The list isn't sorted, but there's another reason we can safely skip.
Start with the widest tank: the first and last walls. Any other tank is narrower. To beat the current tank, a narrower one needs to be taller.
Now look at the shorter of the two walls, say it's the left one. Every other tank using the left wall is narrower, and its water can't rise above the left wall anyway. So no tank with the left wall can beat this one. We can throw that wall away: move left inward.
5. The two-pointer idea
Pointers on the first and last walls. Repeat until they meet:
- Work out this tank's water and keep the best.
- Move the pointer on the shorter wall inward.
Start wide, move the shorter wall
- water =
- 14
- best =
- 14
function biggestTank(heights) { let left = 0; let right = heights.length - 1; let best = 0; while (left < right) { const width = right - left; const height = Math.min(heights[left], heights[right]); best = Math.max(best, width * height); if (heights[left] < heights[right]) { left++; } else { right--; } } return best;}7 apart × 2 high = 14. A new best! The left wall is shorter, so move it inward.
6. The code, line by line
let left = 0; let right = heights.length - 1; let best = 0;Start with the widest tank.
bestholds the most water seen so far.while (left < right) { const width = right - left; const height = Math.min(heights[left], heights[right]); best = Math.max(best, width * height);For the current pair, the width is the distance between them; the height is the shorter wall (
Math.min). Keep the bigger ofbestand this tank.if (heights[left] < heights[right]) { left++; } else { right--; }Throw away the shorter wall by moving its pointer inward. It can't be part of a bigger tank. If the walls are equal, moving either is fine.
return best;Every wall has been considered or ruled out.
7. How fast is it?
| Every pair | Two pointers | |
|---|---|---|
| Time | O(n²) | O(n): one pointer moves per step |
| Extra memory | O(1) | O(1) |
Common mistakes
- Moving the taller wall. That can only make tanks narrower without raising the water: you might skip the answer.
- Using the taller wall for the height. Water spills over the shorter side, so use
Math.min. - Counting the walls between as the width. Walls at positions 1 and 6 are
6 - 1 = 5apart.
Memorise the solution, once you understand it.
This shows the full solution, then helps you rebuild it from memory in four steps. Remembering it is what lets you write it again in an interview, or in a week. Your result goes into your reviews, so it comes back just before you'd forget.
Learn it by heart: Biggest tank
Read the 16 lines slowly. For each one, say out loud what it does and why it is there. Then cover it and move on to step 2.
function biggestTank(heights) { let left = 0; let right = heights.length - 1; let best = 0; while (left < right) { const width = right - left; const height = Math.min(heights[left], heights[right]); best = Math.max(best, width * height); if (heights[left] < heights[right]) { left++; } else { right--; } } return best;}