11. Container With Most Water
mediumAsked at NetflixGiven an integer array of heights, find two lines that together with the x-axis form a container holding the most water. Netflix asks this to confirm you reach for two pointers and can articulate why advancing the shorter side is provably optimal.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Netflix loops.
- Glassdoor (2026-Q1)— Netflix SWE phone-screen reports list this as a 30-minute warm-up before a harder follow-up.
- Blind (2025-12)— Netflix L4-equivalent loop reports mention two-pointer narration as the explicit signal.
Problem
You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]). Find two lines that together with the x-axis form a container, such that the container contains the most water. Return the maximum amount of water a container can store.
Constraints
n == height.length2 <= n <= 10^50 <= height[i] <= 10^4
Examples
Example 1
height = [1,8,6,2,5,4,8,3,7]49Explanation: Lines at index 1 and 8 (heights 8 and 7) form a container holding 7 * (8 - 1) = 49.
Example 2
height = [1,1]1Approaches
1. Brute-force every pair
Try every (i, j) pair; area = (j - i) * min(height[i], height[j]); track the max.
- Time
- O(n^2)
- Space
- O(1)
function maxAreaBrute(height) {
let best = 0;
for (let i = 0; i < height.length; i++) {
for (let j = i + 1; j < height.length; j++) {
const area = (j - i) * Math.min(height[i], height[j]);
best = Math.max(best, area);
}
}
return best;
}Tradeoff: Captures the correct geometry but TLEs at n = 10^5. Mention it to set up the two-pointer narration.
2. Two-pointer (optimal)
Two pointers at both ends. Compute area, then advance whichever pointer is at the shorter line.
- Time
- O(n)
- Space
- O(1)
function maxArea(height) {
let l = 0, r = height.length - 1;
let best = 0;
while (l < r) {
const h = Math.min(height[l], height[r]);
best = Math.max(best, h * (r - l));
if (height[l] < height[r]) l++;
else r--;
}
return best;
}Tradeoff: O(n) and O(1) space. The key invariant: moving the taller side inward can only ever decrease width and is capped at the same shorter height, so it can never improve. Only moving the shorter side has any chance of finding a taller pair.
Netflix-specific tips
Netflix interviewers want you to explicitly justify why moving the shorter pointer is safe. Say: 'The area is bounded by min(left, right) * width. If I move the taller pointer, the new min cannot exceed the current min, and width shrinks — so the area can only get worse. Only moving the shorter side opens the possibility of improvement.' Skipping this proof loses the signal even if the code is correct.
Common mistakes
- Advancing whichever pointer happens to be on the left/right rather than the shorter side.
- Using (r - l + 1) for width instead of (r - l) — the lines are at the endpoints, not over a 1-wide bar.
- Stopping when the shorter side becomes taller — the loop must run until l >= r.
Follow-up questions
An interviewer at Netflix may pivot to one of these next:
- Trapping Rain Water (LC 42) — a related but harder problem where you sum water at every index.
- What if heights could be negative? (They can't here, but discuss how the invariant would change.)
- What if you had a 2D grid of heights? (LC 407, becomes a min-heap BFS.)
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why is moving the shorter pointer provably optimal?
Because area = min(h[l], h[r]) * (r - l). If h[l] < h[r], the area is capped at h[l]. Moving r inward decreases width and keeps the cap at most h[l] (whatever h[r-1] is, the min is still <= h[l]). Only moving l offers a chance to raise the cap.
Does this generalize to 3D (Trapping Rain Water II)?
No, two-pointer breaks in 2D because there's no single 'shorter side' — you need a min-heap BFS from the border inward.
Free learning resources
Curated free links for this problem.
Practice these live with InterviewChamp.AI
Drill Container With Most Water and other Netflix interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →