Skip to main content

34. Container With Most Water

mediumAsked at Reddit

Given heights, find the two lines that form the container holding the most water. Reddit uses this two-pointer problem to test greedy/monotone intuition — analogous to choosing the two endpoints of a time-window to maximize comment volume.

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

Source citations

Public interview reports confirming this problem appears in Reddit loops.

  • Glassdoor (2026-Q1)Reddit phone screen, common medium.

Problem

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, 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. Check all pairs

Nested loop over (i, j); 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. TLE at n=10^5.

2. Two pointers, move shorter (optimal)

Start with full width (l=0, r=n-1). Move whichever pointer points to the shorter line inward. Track max area.

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

Tradeoff: Linear. The proof: moving the taller pointer cannot increase area (width shrinks, height bound by shorter).

Reddit-specific tips

Reddit interviewers want the proof, not just the code. Articulate: 'we always move the shorter pointer because the taller one's height isn't the bottleneck.' Bonus signal: connect to their click-window analysis where they pick two timestamps to maximize a volume metric.

Common mistakes

  • Moving both pointers each iteration.
  • Computing area = max(h[l], h[r]) — should be min.
  • Returning the indices instead of the area.

Follow-up questions

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

  • Trapping Rain Water (LC 42) — same setup, completely different algorithm.
  • What if heights can be negative? (Physically meaningless but instructive.)
  • Largest rectangle in histogram (LC 84).

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 move the shorter pointer?

The container's height is bounded by the shorter line. Moving the taller pointer cannot help because the height stays at the shorter and width shrinks.

Could both pointers ever move?

If heights are equal — moving either is fine. Conventionally move both (or just one) as a tiebreak.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →