Back to Practice
#0233
Container With Most Water
MediumDSA28 min20 XP
Problem
Given line heights, return the maximum area formed by choosing two lines and the x-axis.
Why This Matters
This problem explains the key two-pointer proof: move the limiting side, because moving the taller side cannot improve the current width-height tradeoff.
Function Signature
def max_area(height):
Examples
Example 1
Inputheight = [1, 8, 6, 2, 5, 4, 8, 3, 7]
Output49
The best pair is height 8 at index 1 and height 7 at index 8, width 7, area 49.
Constraints
- Return the exact requested value.
- Handle empty or small inputs when the prompt allows them.
- Prefer the target DSA pattern over brute force when the input can grow.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 3 hidden categories
Classic container
Input[1, 8, 6, 2, 5, 4, 8, 3, 7]
Expected49
The widest useful tall pair gives area 49.
Two lines
Input[1, 1]
Expected1
Width 1 times height 1.
Hidden Test Categories
increasing heightsdecreasing heightslarge plateau