Container With Most Water is the #16 most asked LeetCode problem globally ā the cleaner, simpler cousin of Trapping Rain Water, and the go-to problem for testing greedy two-pointer reasoning.
Our analysis of 10,385 verified interview questions across 259 companies shows it appeared 62 times across 25 companies ā including Google, Amazon, Meta, Microsoft, Goldman Sachs, Citadel, Bloomberg, ByteDance, J.P. Morgan, and Mastercard.
The brute force checks all O(n²) pairs. The optimal solution moves two pointers inward ā always discarding the shorter wall. This greedy choice is provably safe: the shorter wall can never contribute to a larger container with any other partner, since width only decreases as we move inward.
| Metric | Value |
|---|---|
| Total Interview Appearances | 62 verified mentions |
| Companies That Ask It | 25 companies |
| Global Rank | #16 out of 10,385 questions |
| Difficulty | Medium |
| Core Patterns | Array, Two Pointers, Greedy |
Google, Amazon, Meta, Microsoft, Goldman Sachs, Citadel, Bloomberg, ByteDance, J.P. Morgan, Mastercard, Adobe, Apple, Atlassian, Coveo, Deloitte, Flipkart, HSBC, HashedIn, Infosys, Intel, Lenskart, Miro, and Myntra.
LeetCode #11 ā Container With Most Water
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 that contains the most water. Return the maximum amount of water a container can store.
Input: height = [1,8,6,2,5,4,8,3,7] ā Output: 49
(lines at index 1 (height 8) and index 8 (height 7): min(8,7) Ć (8-1) = 49)
Input: height = [1,1] ā Output: 1
Area = min(height[left], height[right]) Ć (right - left). Start with the widest possible container (left=0, right=n-1). To potentially increase the area, we must increase the height ā since width can only shrink as we move inward. Moving the taller wall inward can never help (it was already the bottleneck's limit); only moving the shorter wall gives a chance to find a taller wall that compensates.
Always move the pointer pointing to the shorter wall inward. If both are equal height, move either one.
Suppose height[left] < height[right]. Consider all containers that include the left wall at current position. Every such container uses a right wall that is either the current right or something to the left of it ā meaning a smaller width and a height still limited by height[left]. None can beat the current area. So we can safely discard the left wall and move it right.
Don't move the taller wall. Moving the taller wall only shrinks width while keeping the same height bottleneck ā guaranteed to get worse or equal. Always move the shorter one.
def maxArea(height: list[int]) -> int:
left, right = 0, len(height) - 1
max_water = 0
while left < right:
water = min(height[left], height[right]) * (right - left)
max_water = max(max_water, water)
# Move the shorter wall inward
if height[left] < height[right]:
left += 1
else:
right -= 1
return max_water
class Solution {
public int maxArea(int[] height) {
int left = 0, right = height.length - 1, maxWater = 0;
while (left < right) {
int water = Math.min(height[left], height[right]) * (right - left);
maxWater = Math.max(maxWater, water);
if (height[left] < height[right])
left++;
else
right--;
}
return maxWater;
}
}
var maxArea = function(height) {
let left = 0, right = height.length - 1, maxWater = 0;
while (left < right) {
const water = Math.min(height[left], height[right]) * (right - left);
maxWater = Math.max(maxWater, water);
if (height[left] < height[right])
left++;
else
right--;
}
return maxWater;
};
[1,8,6,2,5,4,8,3,7] step by step to verify the greedy logic.Tag on DSAPrep.dev under Two Pointers and Greedy.