Skip to main content

11. Container With Most Water

mediumAsked at Goldman Sachs

Given heights, find two lines that together with the x-axis form the container with the most water. Goldman Sachs uses Container With Most Water to test the two-pointer technique and the proof that 'always move the shorter side' is correct.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Goldman Sachs loops.

  • Glassdoor (2026-Q1)Goldman Sachs SWE and Strats reports list Container With Most Water as a 'prove the two-pointer is correct' question.
  • LeetCode Discuss (2025-12)Container With Most Water is in the top-15 of LeetCode's Goldman Sachs company tag.

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. Notice that you may not slant the container.

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

Explanation: Lines at indices 1 (height 8) and 8 (height 7); area = min(8,7) * (8-1) = 49.

Example 2

Input
height = [1,1]
Output
1

Approaches

1. Brute-force every pair

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

Time
O(n^2)
Space
O(1)
function maxAreaBrute(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: Times out at n = 10^5. Mention briefly to fix the framing, then move.

2. Two-pointer (optimal)

Start with the widest pair (left=0, right=n-1). Compute area. Move the shorter side inward. Repeat.

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

Tradeoff: Single pass, O(1) space. The proof that moving the shorter side is correct: any pair you could form by moving the taller side has smaller width AND height capped by the same short side — strictly worse.

Goldman Sachs-specific tips

Goldman Sachs interviewers will ask you to PROVE that the two-pointer approach is correct. The proof to memorize: 'Moving the taller side inward can only decrease the width and cannot increase the limiting height. So we lose strictly. Moving the shorter side might find a taller line that compensates for the lost width.' Be ready to draw this on the whiteboard. The proof is the rubric point, not the algorithm.

Common mistakes

  • Moving both pointers each iteration — silently skips half the candidate pairs.
  • Tie-breaker when height[left] === height[right] — either move is fine, but you must pick one. Most candidates handle this implicitly with the else branch.
  • Forgetting that 'water' is bounded by the SHORTER line, not the average.

Follow-up questions

An interviewer at Goldman Sachs may pivot to one of these next:

  • Trapping Rain Water (LC #42) — same array, different question. (Sum of all trapped water, harder.)
  • Largest Rectangle in Histogram (LC #84) — rectangles inside the bars, not on top.
  • What if the lines had thickness? (The math changes — bars displace water.)

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 guarantee we don't miss the optimum?

Because for the current pair (left, right) with height[left] < height[right], any pair (left, right') with right' < right has smaller width AND height bounded by min(height[left], height[right']) <= height[left]. So strictly smaller. The only way to improve is to drop the bottleneck — move left.

What if the two lines are at the same height?

Tie-breaker doesn't matter for correctness. The standard convention is 'move left when heights are equal' but either choice converges to the same optimal answer. Goldman might ask about this to test edge-case awareness.

Free learning resources

Curated free links for this problem.

Practice these live with InterviewChamp.AI

Drill Container With Most Water and other Goldman Sachs interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →