Container With Most Water: Optimal O(n) Solution (Data-Backed Guide)

By DSA Prep Team · March 4, 2026 · 8 min read · Algorithms

Container With Most Water is the #16 most asked LeetCode problem globally — the cleaner, simpler cousin of Trapping Rain Water, and the go-to problem for testing greedy two-pointer reasoning.

Our analysis of 10,385 verified interview questions across 259 companies shows it appeared 62 times across 25 companies — including Google, Amazon, Meta, Microsoft, Goldman Sachs, Citadel, Bloomberg, ByteDance, J.P. Morgan, and Mastercard.

The brute force checks all O(n²) pairs. The optimal solution moves two pointers inward — always discarding the shorter wall. This greedy choice is provably safe: the shorter wall can never contribute to a larger container with any other partner, since width only decreases as we move inward.

Table of Contents

The Data: 25 Companies Ask This

MetricValue
Total Interview Appearances62 verified mentions
Companies That Ask It25 companies
Global Rank#16 out of 10,385 questions
DifficultyMedium
Core PatternsArray, Two Pointers, Greedy

šŸ“Œ Companies That Ask This Problem

Google, Amazon, Meta, Microsoft, Goldman Sachs, Citadel, Bloomberg, ByteDance, J.P. Morgan, Mastercard, Adobe, Apple, Atlassian, Coveo, Deloitte, Flipkart, HSBC, HashedIn, Infosys, Intel, Lenskart, Miro, and Myntra.

Problem Statement

LeetCode #11 — Container With Most Water

You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]). Find two lines that together with the x-axis form a container that contains the most water. Return the maximum amount of water a container can store.

Examples
Input:  height = [1,8,6,2,5,4,8,3,7]   →  Output: 49
        (lines at index 1 (height 8) and index 8 (height 7): min(8,7) Ɨ (8-1) = 49)

Input:  height = [1,1]                  →  Output: 1

The Core Intuition: Always Move the Shorter Wall

Area = min(height[left], height[right]) Ɨ (right - left). Start with the widest possible container (left=0, right=n-1). To potentially increase the area, we must increase the height — since width can only shrink as we move inward. Moving the taller wall inward can never help (it was already the bottleneck's limit); only moving the shorter wall gives a chance to find a taller wall that compensates.

šŸ’” The One-Line Greedy Rule

Always move the pointer pointing to the shorter wall inward. If both are equal height, move either one.

Why the Greedy Choice Is Safe

Suppose height[left] < height[right]. Consider all containers that include the left wall at current position. Every such container uses a right wall that is either the current right or something to the left of it — meaning a smaller width and a height still limited by height[left]. None can beat the current area. So we can safely discard the left wall and move it right.

āš ļø Common Mistake

Don't move the taller wall. Moving the taller wall only shrinks width while keeping the same height bottleneck — guaranteed to get worse or equal. Always move the shorter one.

Code in Python, Java & JavaScript

Python 3
def maxArea(height: list[int]) -> int:
    left, right = 0, len(height) - 1
    max_water = 0

    while left < right:
        water = min(height[left], height[right]) * (right - left)
        max_water = max(max_water, water)

        # Move the shorter wall inward
        if height[left] < height[right]:
            left += 1
        else:
            right -= 1

    return max_water
Java
class Solution {
    public int maxArea(int[] height) {
        int left = 0, right = height.length - 1, maxWater = 0;

        while (left < right) {
            int water = Math.min(height[left], height[right]) * (right - left);
            maxWater = Math.max(maxWater, water);

            if (height[left] < height[right])
                left++;
            else
                right--;
        }
        return maxWater;
    }
}
JavaScript
var maxArea = function(height) {
    let left = 0, right = height.length - 1, maxWater = 0;

    while (left < right) {
        const water = Math.min(height[left], height[right]) * (right - left);
        maxWater = Math.max(maxWater, water);

        if (height[left] < height[right])
            left++;
        else
            right--;
    }
    return maxWater;
};

Complexity Analysis

šŸ“Š Complexity

How to Actually Remember This

āœ… Spaced Repetition Schedule

  1. Today: Solve LC #11. Then trace through [1,8,6,2,5,4,8,3,7] step by step to verify the greedy logic.
  2. Day 3: Solve LC #42 (Trapping Rain Water) — same visual setup, harder problem.
  3. Day 7: Re-implement and explain the greedy proof in one paragraph without notes.

Tag on DSAPrep.dev under Two Pointers and Greedy.

Track on DSAPrep.dev →