Skip to main content

34. Container With Most Water

mediumAsked at Workday

Given an array of heights, find two lines that hold the most water. Workday uses this for two-pointer convergence reasoning — same shape as maximizing fiscal-coverage between two timeline anchors.

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

Source citations

Public interview reports confirming this problem appears in Workday loops.

  • Glassdoor (2025)Workday SDE2 phone screen.

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 that 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. All pairs brute force

Try every (i, j) and compute area.

Time
O(n^2)
Space
O(1)
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 at n=10^5.

2. Two pointers converge

lo=0, hi=n-1. Compute area; move the shorter side inward (only chance to find a taller bound).

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

Tradeoff: Single pass. Moving the shorter line is the only move that could find a taller bound — moving the taller can only narrow.

Workday-specific tips

Workday wants you to justify WHY you move the shorter pointer (not just THAT you do it). The argument: width strictly decreases, so the only way to increase area is to find a taller bound — and only the shorter side has that potential.

Common mistakes

  • Moving the taller pointer — area can only get smaller.
  • Using max instead of min for height — water level is bounded by the shorter wall.
  • Comparing heights with strict < and breaking ties wrong — both directions are fine for ties since you'll cover the case eventually.

Follow-up questions

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

  • Trapping Rain Water (LC 42) — same shape, different question.
  • Maximum Subarray (LC 53).
  • What if walls have width?

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

Width is decreasing every step. If you move the taller wall, the height can only stay the same or drop — you've wasted a step. Moving the shorter wall is the only one that COULD raise the cap.

Does this work for ties?

Yes — when heights are equal, moving either is fine. You won't miss the optimal because both subproblems have the same cap and shrinking width.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →