17. Container With Most Water
mediumAsked at CoupangFind two lines that form the largest water container, mirroring how Coupang's truck-loading planner picks the pair of pallet heights that maximize same-day delivery routing volume.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
Given an integer array height where each value represents a vertical line, find two lines that form a container holding the most water. Return the maximum amount of water it can store.
Constraints
2 <= height.length <= 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
Compute area for every pair.
- Time
- O(n^2)
- Space
- O(1)
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:
2. Two pointers shrinking inward
Start from both ends; move the shorter side inward each step because moving the taller side can never improve area.
- Time
- O(n)
- Space
- O(1)
function maxArea(height) {
let l = 0, r = height.length - 1, 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:
Coupang-specific tips
Coupang's truck-loading planner picks pallet-height pairs that maximize cubic capacity for same-day delivery routing; two-pointer shrinking is the canonical pattern over O(n^2) scans on dispatch-time data.
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 Coupang interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →