#11 Container With Most Water
frequently reported atWalmartAmazonFlipkartBloombergTCSMicrosoft
the problem
In plain words
height[i] is a vertical wall at x = i. Pick two walls that, with the x-axis, hold the most water. Return that maximum area.
in height = [1,8,6,2,5,4,8,3,7]
out 49
Walls at index 1 (8) and 8 (7): min(8,7) × 7 = 49.
in height = [1,1]
out 1
- 2 ≤ n ≤ 10⁵
- 0 ≤ height[i] ≤ 10⁴
your turn
Try it before you peek
Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.
Solve it in the browser
LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.
Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.
Marking the problem solved, or opening every hint, also shows it.
the aha
Key insight
patternTwo pointers (opposite ends)
Area = width × shorter wall. Start with maximum width. Moving the taller wall can't raise the shorter wall and only shrinks width — so always move the shorter one.
solutions
From brute force to optimal
Compute the area for all pairs.
- Two loops, area = (j − i) × min(h[i], h[j]).
All pairs.
One running max.
class Solution { public int maxArea(int[] height) { int best = 0; for (int i = 0; i < height.length; i++) for (int j = i + 1; j < height.length; j++) best = Math.max(best, (j - i) * Math.min(height[i], height[j])); return best; }}Start widest; after measuring, discard the shorter wall.
- left = 0, right = n−1.
- best = max(best, width × min height).
- Move the pointer at the shorter wall inward.
Each step discards one wall.
Two pointers and a max.
class Solution { public int maxArea(int[] height) { int left = 0, right = height.length - 1, best = 0; while (left < right) { int h = Math.min(height[left], height[right]); best = Math.max(best, h * (right - left)); // The shorter wall limits the water. Moving the taller wall can only // shrink the width without raising the limit — so move the shorter one. if (height[left] < height[right]) left++; else right--; } return best; }}Dry run
in the room
How to say it in the interview
talk track
- Prove the move: "Any container that keeps the shorter wall is narrower and no taller, so it can't beat what we just measured."
traps
- Confusing this with Trapping Rain Water — here only two walls matter; walls in between don't block anything.
what they ask next
Follow-ups
Why is it safe to move either pointer when both walls are equal?
Any pair that keeps one of them is narrower and still capped at that same height, so neither wall can do better. Dropping either loses nothing.
Return the two indices instead of the area?
Same scan — whenever you update best, also save left and right. Still O(n) time, O(1) space.
before you submit
Edge cases to test
- Two walls only: the answer is min(h[0], h[1]) × 1.
- Zero-height walls hold nothing: [0, 0] → 0.
- Equal walls at both ends, like [4, 3, 2, 1, 4] → 16: moving either side is safe.
- 10⁵ walls: every pair is about 5·10⁹ areas; two pointers is at most 10⁵ − 1 steps.
practise next
Similar problems
- #42Trapping Rain WaterHardin Two Pointers
- #84Largest Rectangle in HistogramHardin Stacks