Coding interview problems › Two Pointers
↔️ Two Pointers Medium
Container With Most Water
Container With Most Water explained: start with the widest pair and always move the shorter wall, a greedy two-pointer proof in O(n). Tested Python, visual.
Also searched as: container with most water two pointers · max water between two lines
Try it live Free
Two Pointers
This is the lab's own example: two walls bound the area, and you watch the shorter wall move every step. Try to find a case where moving the taller wall would have been better; there is none.
The problem
You are given a list of non-negative heights; line i stands at position i with height height[i]. Pick two lines that, together with the x-axis, hold the most water. The water held is the distance between them times the shorter of the two heights.
Lines at positions 1 and 8: width 7, height min(8, 7) = 7.
The key insight
Start with the widest container, the two ends. The area is limited by the shorter wall. Moving the taller wall inward can only make the width smaller while the height stays capped by the same short wall, so it can never help. Moving the shorter wall is the only move that might find a taller limit.
Step-by-step approach
- Set
i = 0,j = n - 1,best = 0. - Compute
area = (j - i) * min(height[i], height[j])and updatebest. - Move the pointer at the shorter line inward (either one if equal).
- Repeat while
i < jand returnbest.
Python solution
def max_area(height):
i, j = 0, len(height) - 1
best = 0
while i < j:
h = min(height[i], height[j])
best = max(best, (j - i) * h)
if height[i] < height[j]:
i += 1 # the shorter wall limits us: move it
else:
j -= 1
return bestComplexity
Each step moves one pointer inward, so there are at most n - 1 steps and only a few variables.
Common mistakes
- Moving the taller wall, which can skip the optimal pair.
- Confusing this with Trapping Rain Water: here only two lines matter and the lines in between are ignored.
- Using
j - i + 1as the width; the distance between positions i and j isj - i.
Frequently asked questions
What is the time and space complexity of Container With Most Water?
The two-pointer solution is O(n) time and O(1) space. The brute force over all pairs is O(n²).
Why is moving the shorter line always correct?
Any container that keeps the shorter line and uses a closer partner has smaller width and height at most that shorter line, so it is no better. That shorter line can be discarded safely.
What if both lines are equal height?
Move either one. Any better container must use neither of them, because its height would still be capped at or below that shared height with a smaller width.
Preparing for placements?
Take the 2-minute readiness check to find your weak patterns, then follow the placement path from DSA core to system design.
All coding interview problems More Two Pointers Two Pointers lab Placement prep guides
Problem statements on this site are written in our own words. Problem names are the common names used across interview-prep communities; BitBootcamp is an independent learning platform and is not affiliated with LeetCode or any company that asks these questions.