MediumArrayTwo PointersGreedyUpdated Sep 2026

11. Container With Most Water

Asked at Google, Meta, Amazon, Microsoft and 6 more

Difficulty

Medium

Time

O(n)

Space

O(1)

Pattern

Array

Companies

10

Problem

You are given an array height. Think of each value as the height of a vertical wall standing at that index. The bottom of every wall sits on the x-axis, so wall i runs from point (i, 0) up to (i, height[i]).

Pick any two walls. Together with the x-axis they form a container. The water it can hold is min(height[i], height[j]) * (j - i): the shorter wall decides how high the water can rise, and the distance between the walls is the width. The container must stay upright — you may not tilt it.

Return the largest amount of water any pair of walls can hold.

Two walls plus the x-axis make a container

heightwidth 7186254837012345678Walls at index 1 and index 8: height 7 × width 7 = 49
Water is capped by the shorter wall, and the width is the gap between the two walls.

Constraints

  • ●2 <= height.length <= 10^5
  • ●0 <= height[i] <= 10^4

Examples

Example 1
Inputheight = [1,8,6,2,5,4,8,3,7]
Output49
WhyThe best pair is index 1 (height 8) and index 8 (height 7). The water level is limited by the shorter wall, 7, and the width is 8 - 1 = 7. So the area is 7 × 7 = 49.
Example 2
Inputheight = [1,1]
Output1
WhyThere are only two walls, so they are the only choice. The shorter height is 1 and the width is 1, giving 1 × 1 = 1.

Asked At

10 companies

How to Think About It

1

Brute force: try every pair of lines. For each pair (i, j), area = min(height[i], height[j]) × (j - i). That's O(n²). Two pointers bring it to O(n).

2

Start with the widest container: left pointer at index 0, right pointer at index n-1. The area is limited by the shorter line. Width is maximized.

3

Key insight: to find a larger area, you need either a taller line OR a wider container. Since width only decreases as you move pointers inward, you need a taller line to compensate. So always move the shorter pointer — the taller one might give you a bigger area.

4

Why moving the taller pointer is wasteful: if you move the taller pointer, the new width is smaller, and the height is still limited by the shorter line. You can never improve. But if you move the shorter pointer, you might find a taller line that compensates for the lost width.

Optimal Approach

Place left pointer at 0, right pointer at n-1. Calculate area = min(height[left], height[right]) × (right - left). Update max area. Move the pointer at the shorter line inward.

The invariant: at each step, all pairs involving the shorter line and any line between the pointers have been considered (or are guaranteed to be smaller). Moving the shorter line is safe.

Time: O(n) — each pointer moves at most n times. Space: O(1).

Two pointers, move the shorter wall

height186254837012345678lohiStep 1 — lo=0, hi=8: min(1,7) × 8 = 8. The left wall is shorter, so move loright.height186254837012345678lohiStep 2 — lo=1, hi=8: min(8,7) × 7 = 49. New best. The right wall is shorter,so move hi left.height186254837012345678lohiStep 3 — lo=1, hi=6: min(8,8) × 5 = 40. Still below 49, so keep movinginward.area = min(height[lo], height[hi]) × (hi − lo)move the shorter side inwardanswer = 49
Start as wide as possible, then always move the shorter side inward to look for a taller wall.
ComplexityTime O(n)Space O(1)Each pointer moves inward at most n times total, and we only keep a few variables.

What Trips People Up in Real Interviews

1

Trying to use brute force (check all pairs). That's O(n²) and will time out. The two-pointer approach is O(n) and is what the interviewer expects.

2

Moving the pointer with the larger height. That's wrong — you should move the pointer with the smaller height. Moving the larger one can only decrease the area (shorter width, height can't increase beyond the smaller).

3

Forgetting that the area is min(height[left], height[right]) × (right - left). The height is limited by the shorter wall, not the taller one.

4

Not initializing the result to 0. If no container has positive area (all heights are 0), return 0.

5

Initializing the result to -1 instead of 0. Area is always non-negative since heights are non-negative, so the minimum possible answer is 0, not -1.

Solution Code

O(n) time
def maxArea(height):
    lo, hi = 0, len(height) - 1
    best = 0
    while lo < hi:
        area = min(height[lo], height[hi]) * (hi - lo)
        best = max(best, area)
        if height[lo] < height[hi]:
            lo += 1
        else:
            hi -= 1
    return best

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Container With Most Water problem?

Given an array of heights, find two lines that together with the x-axis form a container that holds the most water. This problem tests your ability to use two pointers and reason about why a greedy approach works.

How do you solve Container With Most Water?

The optimal approach runs in O(n) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Container With Most Water?

Container With Most Water is asked at Google, Meta, Amazon, Microsoft, Oracle, Salesforce, Walmart, Bloomberg, Goldman Sachs, Flipkart. It is a medium difficulty problem.

What are common mistakes on Container With Most Water?
  • Trying to use brute force (check all pairs). That's `O(n²)` and will time out. The two-pointer approach is `O(n)` and is what the interviewer expects.
  • Moving the pointer with the larger height. That's wrong — you should move the pointer with the smaller height. Moving the larger one can only decrease the area (shorter width, height can't increase beyond the smaller).
  • Forgetting that the area is min(`height[left]`, `height[right]`) × (right - left). The height is limited by the shorter wall, not the taller one.
  • Not initializing the result to 0. If no container has positive area (all heights are 0), return 0.
  • Initializing the result to -1 instead of 0. Area is always non-negative since heights are non-negative, so the minimum possible answer is 0, not -1.