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.

Open the playground →

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.

Inputheight = [1, 8, 6, 2, 5, 4, 8, 3, 7]
Output49

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.

Brute force first: Checking every pair of lines is O(n²).

Step-by-step approach

  1. Set i = 0, j = n - 1, best = 0.
  2. Compute area = (j - i) * min(height[i], height[j]) and update best.
  3. Move the pointer at the shorter line inward (either one if equal).
  4. Repeat while i < j and return best.

Python solution

PYTHON 3tested
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 best

Complexity

TimeO(n)
SpaceO(1)

Each step moves one pointer inward, so there are at most n - 1 steps and only a few variables.

Common mistakes

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.