Skip to main content

34. Container With Most Water

mediumAsked at Salesforce

Find two lines that, with the x-axis, form a container holding the most water. Salesforce uses this to test the converging two-pointer pattern with an optimality argument.

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

Source citations

Public interview reports confirming this problem appears in Salesforce loops.

  • Glassdoor (2026-Q1)Salesforce uses this to test greedy correctness arguments.
  • LeetCode Discuss (2025-10)Asked specifically to gauge two-pointer reasoning.

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 pairs

Try every pair (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: TLE on 10^5 inputs. Salesforce wants O(n).

2. Two pointers converging

Start at the ends; compute area; move the shorter side inward (taller side could only help, shorter side caps the 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);
    best = Math.max(best, area);
    if (height[l] < height[r]) l++;
    else r--;
  }
  return best;
}

Tradeoff: O(n) time. The greedy correctness argument: moving the taller side inward CAN'T increase the area (width strictly decreases and the cap is still the shorter side).

Salesforce-specific tips

Salesforce specifically asks for the correctness PROOF of why moving the shorter side is safe. Bonus signal: argue that moving the taller side strictly decreases the area (width down, cap unchanged), so the shorter side is the only useful move. They want this articulated, not just the code.

Common mistakes

  • Moving the side with smaller value but breaking ties arbitrarily without justification.
  • Computing area as h * (j - i + 1) — width is (j - i), not (j - i + 1).
  • Not stopping at l < r — would compare a line to itself, giving 0 area.

Follow-up questions

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

  • Trapping Rain Water (LC 42) — similar two-pointer but trapping water between bars.
  • Largest Rectangle in Histogram (LC 84).
  • What if the bars have width > 1?

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 is moving the shorter side correct?

The current area is capped by the shorter side. Moving the shorter inward might increase the cap (or not). Moving the taller inward can't increase the cap (it's still capped by the shorter) AND decreases the width. So always move the shorter side.

What if both sides are equal?

Moving either is safe — they're symmetric. Doesn't matter which you pick.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →