Skip to main content

34. Container With Most Water

mediumAsked at Datadog

Find two heights that form a container holding the most water. Datadog asks this for the two-pointer-greedy proof — the same kind of monotone-invariant reasoning needed for windowed peak detection on metric streams.

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

Source citations

Public interview reports confirming this problem appears in Datadog loops.

  • Glassdoor (2026-Q1)Datadog onsite — graded on the greedy proof.
  • LeetCode Discuss (2025-09)Datadog tagged.

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. Brute force all pairs

Two nested loops computing area for each (i, j).

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 — fails on 10^5 inputs.

2. Two pointers with greedy shrink (optimal)

Start with the widest possible container. Move the SHORTER side inward — moving the taller side can only decrease area.

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

Tradeoff: Single pass. The key insight — moving the shorter side is the ONLY chance to find a larger area — is what Datadog grades on.

Datadog-specific tips

Datadog will press you to PROVE why moving the shorter side is correct. The proof: if we move the taller side, the width shrinks AND the min(height) can't grow (still capped by the shorter side). So we'd strictly lose. Articulate this before coding.

Common mistakes

  • Moving both pointers in tandem — skips potential answers.
  • Moving the taller side — provably suboptimal.
  • Computing area as (hi - lo + 1) — off by one; the width is (hi - lo), not the count of indices.

Follow-up questions

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

  • Trapping Rain Water (LC 42) — same two-pointer skeleton, harder accounting.
  • Largest Rectangle in Histogram (LC 84) — monotonic stack variant.
  • Container With 3 Sides — generalize to a different shape.

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 side?

The area is min(h[lo], h[hi]) * (hi - lo). Moving the shorter side might raise the min; moving the taller side cannot, because the min is still capped by the shorter side.

What if heights are equal?

Move either. The proof is symmetric — neither choice can improve the area faster than the other.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →