19. Container With Most Water
mediumAsked at PayPalFind two vertical lines that together with the x-axis form a container holding the most water. PayPal uses this two-pointer classic to test greedy reasoning and candidates' ability to prove why a pointer-shrink strategy is optimal — a proof skill valued in financial algorithm design.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in PayPal loops.
- LeetCode Discuss (2025)— PayPal SWE candidate mentioned container problem in technical phone screen recap
- Blind (2026)— PayPal interview loop report included two-pointer problems in the algorithm round
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 holds the most water. Return the maximum amount of water a container can store.
Constraints
n == height.length2 <= n <= 1000000 <= height[i] <= 10000
Examples
Example 1
height = [1,8,6,2,5,4,8,3,7]49Explanation: Lines at index 1 (height 8) and index 8 (height 7) give area = min(8,7) * (8-1) = 7 * 7 = 49
Example 2
height = [1,1]1Approaches
1. Brute force (all pairs)
Try every pair (i, j), compute area = min(height[i], height[j]) * (j - i), track the maximum.
- Time
- O(n^2)
- Space
- O(1)
function maxArea(height) {
let max = 0;
for (let i = 0; i < height.length; i++) {
for (let j = i + 1; j < height.length; j++) {
max = Math.max(max, Math.min(height[i], height[j]) * (j - i));
}
}
return max;
}Tradeoff: O(n^2) — TLE on 100k input. Mention it to establish the area formula before optimizing.
2. Two-pointer greedy
Start with the widest possible container (pointers at both ends). Moving the taller pointer inward can never increase the area (width decreases AND height is still bounded by the shorter line). Always move the shorter pointer inward — the only chance to find a greater area is finding a taller short side.
- Time
- O(n)
- Space
- O(1)
function maxArea(height) {
let left = 0;
let right = height.length - 1;
let maxWater = 0;
while (left < right) {
const h = Math.min(height[left], height[right]);
const area = h * (right - left);
maxWater = Math.max(maxWater, area);
// Move the pointer at the shorter line inward
if (height[left] <= height[right]) {
left++;
} else {
right--;
}
}
return maxWater;
}Tradeoff: The greedy proof is the key interview moment: moving the taller pointer can only decrease both width and effective height, so we lose nothing by always advancing the shorter pointer.
PayPal-specific tips
PayPal interviews focus on payment processing, fraud detection logic, financial reconciliation algorithms, and distributed transaction design. Hash maps, sliding windows, and two-pointer techniques appear frequently.
Common mistakes
- Moving the taller pointer instead of the shorter — breaks the greedy invariant and can miss the optimum
- Computing area as height[left] * (right - left) instead of min(height[left], height[right]) * (right - left)
- Not being able to articulate WHY moving the shorter pointer is safe — interviewers at PayPal ask for the proof
Follow-up questions
An interviewer at PayPal may pivot to one of these next:
- What if you can remove at most one line to maximize water? (Greedy + prefix/suffix max heights)
- Trapping Rainwater — water trapped between bars, not just the outer container (LC 42)
- 3D variant: given a grid, find the maximum volume of water that can be trapped
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why is moving the taller pointer always suboptimal?
Area = min(left, right) * width. If we move the taller pointer, width decreases by 1 and the effective height is still capped by the shorter line — area can only decrease or stay the same. Moving the shorter pointer is our only hope for improvement.
Does this greedy always find the global maximum?
Yes. The proof by contradiction shows every skipped pair (i, j) where height[i] < height[j] has a lower area than the current max at the time we advance pointer i. A formal induction proof impresses PayPal interviewers.
Practice these live with InterviewChamp.AI
Drill Container With Most Water and other PayPal interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →