Skip to main content

34. Container With Most Water

mediumAsked at Vercel

Given a set of vertical lines, find the two that hold the most water between them. Vercel asks this for the two-pointer optimization — and because the 'always move the shorter side' invariant is the same shape as their bandwidth-allocation greedy.

By Sam K., Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Vercel loops.

  • Glassdoor (2025-12)Vercel onsite; the move-shorter-side proof is the bonus.
  • Blind (2026-Q1)Listed in Vercel runtime engineer screen.

Problem

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

Constraints

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

Examples

Example 1

Input
height = [1,8,6,2,5,4,8,3,7]
Output
49

Example 2

Input
height = [1,1]
Output
1

Approaches

1. Brute force all pairs

Try every pair (i, j); compute area = min(h[i], h[j]) * (j - i).

Time
O(n^2)
Space
O(1)
function maxArea(height) {
  let best = 0;
  for (let i = 0; i < height.length; i++) {
    for (let j = i + 1; j < height.length; j++) {
      best = Math.max(best, Math.min(height[i], height[j]) * (j - i));
    }
  }
  return best;
}

Tradeoff: Quadratic. Vercel will ask you to do better.

2. Two-pointer move-shorter (optimal)

Pointers at both ends. Compute area. Move the pointer at the SHORTER line inward — moving the taller can only decrease both width and possibly height.

Time
O(n)
Space
O(1)
function maxArea(height) {
  let l = 0, r = height.length - 1;
  let best = 0;
  while (l < r) {
    const h = Math.min(height[l], height[r]);
    best = Math.max(best, h * (r - l));
    if (height[l] < height[r]) l++;
    else r--;
  }
  return best;
}

Tradeoff: O(n) single pass. The 'move the shorter side' choice is provably correct: moving the taller can never increase the area because the bottleneck (shorter) stays the same.

Vercel-specific tips

Vercel grades the two-pointer AND the correctness proof. Bonus signal: explaining the 'move shorter' invariant out loud BEFORE coding — moving the taller pointer can't increase area because the min stays the same and the width decreases. If both heights are equal, either pointer works.

Common mistakes

  • Moving both pointers — skips valid candidates.
  • Moving the TALLER side — wrong, never finds the optimum.
  • Computing area as max instead of min of the two heights — water levels at the shorter.

Follow-up questions

An interviewer at Vercel may pivot to one of these next:

  • Trapping Rain Water (LC 42, hard) — related two-pointer pattern.
  • Largest Rectangle in Histogram (LC 84, hard).
  • What if there are >2 walls (3D container)?

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why does moving the shorter side work?

The area is bounded by min(left, right) * width. If you move the taller side inward, the min can only stay the same or decrease, AND the width decreases. So no chance of improvement. Moving the shorter MIGHT find a taller line and increase min enough to overcome the lost width.

What if heights are equal?

Either pointer works; the area is the same on both sides. Common convention is to move left.

Companies that also ask Container With Most Water