13. Container With Most Water
mediumAsked at RevolutFind two lines forming the maximum-area container via a two-pointer walk, a Revolut screen that mirrors picking the best buy/sell pair in an FX order book.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
Given n non-negative integers representing heights of vertical lines on a 2D plane, find two lines that together with the x-axis form a container holding the most water. Return the maximum area.
Constraints
2 <= 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. All pairs
Try every pair and track max area.
- Time
- O(n^2)
- Space
- O(1)
let best = 0;
for (let i=0;i<n;i++) for (let j=i+1;j<n;j++) best = Math.max(best, Math.min(h[i],h[j])*(j-i));Tradeoff:
2. Two pointers
Start at edges, move the shorter side inward; only that move can ever increase the area. Linear time.
- Time
- O(n)
- Space
- O(1)
function maxArea(height){
let l = 0, r = height.length - 1, 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:
Revolut-specific tips
Revolut interviewers want you to prove WHY shrinking from the shorter side is safe — explicitly state the invariant before coding, the same way you would prove an FX-pair monotonicity claim.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
Practice these live with InterviewChamp.AI
Drill Container With Most Water and other Revolut interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →