Find two lines that hold the most water with a two-pointer squeeze in O(n), and learn the greedy proof that moving the shorter line is always safe.
The problem
heights[i] is the height of a vertical line at position i. Choose two lines that, together with the x-axis, hold the most water. Return that area: min(h[i], h[j]) * (j - i).
Examples
Example 1
Input
max_area([1, 8, 6, 2, 5, 4, 8, 3, 7])
Expected output
49
Example 2
Input
max_area([1, 1])
Expected output
1
+ 3 hidden tests on Submit.
Edge cases to ask about
- Two lines
- Equal heights
- One line
Hints
0/3How an interviewer scores this
0/9Your code runs in real CPython inside your browser — nothing is sent anywhere. The first run downloads the interpreter (about 6 MB, once). Your code is saved on this device as you type.
Complexity Lab
What does this cost as n grows?
Interviewers score the analysis as much as the code. Commit to an answer first — then check it, and measure your code against the optimal one at growing input sizes.
Pick both to reveal the answer.
Measure it
Runs the function on inputs of size 250 up to 16,000 and records the time and peak memory. Slow solutions stop early — a short curve is itself the answer.
From brute force to optimal
The progression an interviewer wants to hear, one step at a time.
| Approach | Time | Space | Idea |
|---|---|---|---|
| Every pair | O(n²) | O(1) | |
| bestSqueeze from both ends | O(n) | O(1) | Always move the shorter line inward. |
Walkthrough of the optimal approach (try it yourself first)
Start with the widest container (lo = 0, hi = n − 1). The water level is set by the shorter line. Moving the taller line inward makes the width smaller while the height is still capped by the shorter one, so it can never improve the area. So always move the shorter line.
That exchange argument is what interviewers want you to say out loud.
Complexity: O(n) time, O(1) space. Each step moves one pointer inward, so there are at most n − 1 steps.
Reveal the reference solution
def max_area(heights): lo, hi = 0, len(heights) - 1 best = 0 while lo < hi: best = max(best, min(heights[lo], heights[hi]) * (hi - lo)) if heights[lo] < heights[hi]: lo += 1 else: hi -= 1 return best
The brute force, for comparison
def max_area(heights): best = 0 for i in range(len(heights)): for j in range(i + 1, len(heights)): best = max(best, min(heights[i], heights[j]) * (j - i)) return best
Follow-ups interviewers ask
- Prove the greedy move is safe.
- Trapping rain water (next question).
Frequently asked interview questions
Core interview concepts, complexities, and follow-ups scored by hiring teams.
What is the time complexity of Container With Most Water in Python?
The optimal solution runs in O(n) time and O(1) auxiliary space. Each step moves one pointer inward, so there are at most n − 1 steps.
What is the brute-force approach, and how do you optimise it?
Every pair: O(n²) time, O(1) space. Squeeze from both ends: O(n) time, O(1) space. Always move the shorter line inward.
What follow-up questions do interviewers ask about Container With Most Water?
Prove the greedy move is safe. Trapping rain water (next question).
