34. Container With Most Water
mediumAsked at SnowflakeFind two lines forming the largest container of water. Snowflake uses this to test whether you can argue the correctness of the two-pointer move — the same kind of monotonic reasoning that justifies join-order pruning in their planner.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Snowflake loops.
- Glassdoor (2025-Q4)— Snowflake new-grad onsite paired with monotonicity-argument follow-up.
- LeetCode Discuss (2025-11)— Reported at Snowflake intern screens.
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. Notice that you may not slant the container.
Constraints
n == height.length2 <= n <= 10^50 <= height[i] <= 10^4
Examples
Example 1
height = [1,8,6,2,5,4,8,3,7]49Example 2
height = [1,1]1Approaches
1. Brute force pairs
Try every pair (i, j); compute min(height[i], height[j]) * (j - i).
- Time
- O(n^2)
- Space
- O(1)
function maxArea(height) {
let best = 0;
for (let i = 0; i < height.length; i++) {
for (let j = i + 1; j < height.length; j++) {
best = Math.max(best, Math.min(height[i], height[j]) * (j - i));
}
}
return best;
}Tradeoff: Quadratic. Mention only as the no-thinking baseline.
2. Two pointers from both ends (optimal)
Left and right at the extremes. At each step, area = min(h[l], h[r]) * (r - l). Move the shorter side inward — moving the taller never improves area.
- Time
- O(n)
- Space
- O(1)
function maxArea(height) {
let l = 0, r = height.length - 1;
let best = 0;
while (l < r) {
const area = Math.min(height[l], height[r]) * (r - l);
if (area > best) best = area;
if (height[l] < height[r]) l++;
else r--;
}
return best;
}Tradeoff: Linear. The proof: moving the taller side only shrinks width without helping height — so the only chance of improvement is by moving the shorter side.
Snowflake-specific tips
Snowflake interviewers want the monotonicity proof verbalized: 'moving the taller side guarantees a smaller area, so it cannot be optimal'. Bonus signal: connect to join-order pruning — the planner discards subplans it can prove can't improve over the best-so-far using similar monotonic cost reasoning.
Common mistakes
- Moving the taller pointer — loses the monotonic correctness argument and can miss the optimum.
- Moving both pointers when heights are equal — fine for correctness, but explain that you skip one.
- Returning the length instead of the area.
Follow-up questions
An interviewer at Snowflake may pivot to one of these next:
- Trapping Rain Water (LC 42) — different but related two-pointer pattern.
- Why can't this be solved with DP?
- How does Snowflake's planner prune join orders?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why move the shorter side?
Width must shrink as pointers move inward. So the only way the new area beats the current is if the new height is taller. Moving the shorter pointer is the only move that could yield a taller min.
What if both heights are equal?
Either move works. The standard convention is to move left, but the algorithm stays correct either way.
Practice these live with InterviewChamp.AI
Drill Container With Most Water and other Snowflake interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →