Skip to main content

34. Container With Most Water

mediumAsked at Plaid

Find two lines that, together with the x-axis, form the container with the most water. Plaid asks this as a two-pointer optimization classic before harder window problems on balance-history charts.

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

Source citations

Public interview reports confirming this problem appears in Plaid loops.

  • Glassdoor (2025)Plaid SWE II OA.
  • LeetCode Discuss (2026)Plaid two-pointer warm-up.

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. Try every pair

Nested loop over all (i, j) pairs.

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, (j - i) * Math.min(height[i], height[j]));
    }
  }
  return best;
}

Tradeoff: TLE on n=10^5. Mention as the obvious starting point.

2. Two pointers from both ends

Lo and hi pointers. Area = (hi-lo) * min(h[lo], h[hi]). Move the shorter pointer inward — that's the only way to potentially find a better area.

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

Tradeoff: Linear time, constant space. The key insight: moving the taller pointer inward can never help because width shrinks and height is capped by the shorter side.

Plaid-specific tips

Plaid grades this on whether you articulate the 'move the shorter side' invariant. Without that justification, the algorithm looks like a guess. Bonus signal: prove the optimality — if we move the taller side, width decreases AND the height is still capped by the shorter side, so the new area is strictly worse.

Common mistakes

  • Moving both pointers — drops potentially-best pairs.
  • Computing min(left, right) wrong — using max instead of min.
  • Comparing area instead of choosing pointer to move.

Follow-up questions

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

  • Trapping rain water (LC 42) — same shape, different question.
  • Container with 3 walls.
  • What if water can leak — drainage analysis.

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 current area is bottlenecked by the shorter side. Moving the taller side keeps the same bottleneck but loses width — the new area is strictly smaller. Only moving the shorter side has any chance to find a taller bottleneck.

Why is this O(n) and not O(n log n)?

Each iteration moves exactly one pointer, and pointers only move inward. Total moves <= n.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →