Skip to content

Interview Solver

FeaturesPricingHelp
Sign In
FeaturesPricingHelpSign In
  1. Interview Questions
  2. /
  3. Problems
  4. /
  5. Container With Most Water

Container With Most Water

Choose two vertical lines that, with the x-axis, hold the most water. The textbook case for closing two pointers in from both ends. Solve it below in Python or JavaScript, then read the walkthrough.

MediumArrayTwo PointersGreedyAsked at 153 companies incl. Google, Amazon, Meta

Problem

You're given an array height where height[i] is the height of a vertical line standing at position i on the x-axis.

Pick two lines. Together with the x-axis they form a container, and the water it holds is the distance between the lines times the height of the shorter line. Return the most water any pair of lines can hold. The lines between your two choices don't get in the way.

Example 1

Input
height = [2, 7, 3, 8, 4, 6]
Output
24

Lines at positions 1 and 5 have heights 7 and 6. The shorter is 6 and they are 4 apart, so 6 × 4 = 24.

Example 2

Input
height = [5, 5]
Output
5

Example 3

Input
height = [1, 9, 1, 1, 1, 1, 1, 9]
Output
54

The two 9s are 6 apart. Using the outermost lines is wider but only 1 tall.

Constraints

  • 2 ≤ height.length ≤ 10⁵
  • 0 ≤ height[i] ≤ 10⁴

Practice

1
2
3
4

Runs 12 tests in your browser. Python loads on the first run (about 6 MB, then cached). Press ⌘/Ctrl + Enter to run.

Get asked Container With Most Water in a real interview?

Interview Solver gives you a working solution live, during your actual coding interview. Invisible to screensharing.

Start Free Trial

How to solve Container With Most Water

Pattern: Two pointers from both ends

Start with the widest container possible: the first and last lines. Any other container is narrower, so the only way to beat it is with a taller short side.

Now ask which pointer to move inward. The water is limited by the shorter line. If you keep the shorter line and move the taller one inward, the width shrinks and the height can't rise above that same shorter line, so every such container is worse. That means the shorter line has already been paired with the best partner it will ever get, and you can discard it. Move its pointer inward and repeat.

Each step throws away one line for good, so you check n - 1 containers instead of all n²/2 pairs without ever skipping the best one.

Approach 1: Brute force: every pair

Try every i < j, compute min(height[i], height[j]) * (j - i), and keep the largest. Easy to get right, but quadratic, which is too slow at 10⁵ lines.

Time O(n²) · Space O(1)

Approach 2: Two pointers, always moving the shorter side

Set left = 0 and right = n - 1. While left < right:

  1. Compute the area for this pair and update the best.
  2. Move whichever pointer points at the shorter line one step inward. On a tie, moving either is fine: with both sides equal, no container that keeps either one can be taller than the current one, and every such container is narrower.

The argument above is the proof: the line you move away from can't be part of any better container still to be checked.

Time O(n) · Space O(1)

ApproachTimeSpace
Brute force: every pairO(n²)O(1)
Two pointers, always moving the shorter sideO(n)O(1)

Solution code

Try the practice editor first. Both solutions pass every test above.

▸Container With Most Water solution in Python
def max_area(height: list[int]) -> int:
    left, right = 0, len(height) - 1
    best = 0
    while left < right:
        best = max(best, min(height[left], height[right]) * (right - left))
        if height[left] < height[right]:
            left += 1
        else:
            right -= 1
    return best
▸Container With Most Water solution in JavaScript
function maxArea(height) {
  let left = 0;
  let right = height.length - 1;
  let best = 0;
  while (left < right) {
    best = Math.max(best, Math.min(height[left], height[right]) * (right - left));
    if (height[left] < height[right]) left++;
    else right--;
  }
  return best;
}

Common mistakes

  • Moving the taller pointer, which can skip past the optimal pair.
  • Using the taller line's height, or the sum of heights, instead of the shorter one.
  • Measuring width as `right - left + 1`. The lines are at positions `left` and `right`, so the width is `right - left`.
  • Confusing this with Trapping Rain Water. Here the lines in between are ignored; only the two chosen walls matter.

What interviewers look for

Most candidates can state the two-pointer rule; what interviewers listen for is the justification. Explain that keeping the shorter line while shrinking the width can never help, so that line is finished. Saying this clearly is the difference between "I remember the trick" and "I can prove it works."

Start by naming the brute force and its O(n²) cost, then present the narrowing argument. Trace one example with the pointers to show the shorter side moving.

Follow-up questions

Why is it safe to move either pointer when the heights are equal?

With both lines at height h, any container that keeps one of them is narrower and still capped at h, so it can't beat the current one. The only way to improve is to replace both, and the pointers will get there either way.

Can you skip ahead faster in practice?

After moving the shorter pointer, keep moving it while the new line is no taller than the one you just left, since those containers are narrower and no taller. It doesn't change the O(n) bound, but it skips useless work.

What if you have to return the pair of indices instead of the area?

Store `left` and `right` whenever you update the best area. If several pairs tie, decide on a rule up front, such as the first pair found, and say so.

Companies that ask Container With Most Water

Container With Most Water is tagged by 153 companies in our interview dataset. The score shows how often it comes up at each company.

  • Regeneron100%
  • Pubmatic100%
  • Lockheed Martin99%
  • Chronosphere98%
  • Costco Wholesale95%
  • Applovin94%
  • L3harris93%
  • Workday92.1%
  • Seagate91%
  • Wixcom90.4%
  • Careem89.1%
  • SAP88.4%
  • Avaya88%
  • Datavant88%
  • Pornhub86.5%
  • Gocardless86%
  • Contentful86%
  • Exact Sciences85%
  • Farfetch84%
  • Mailchimp83%
  • Alaska Airlines82%
  • Freshworks80.8%
  • Klarna80%
  • Outreach79%

and 129 more.

Practice next

  • Trapping Rain WaterHardTwo pointers with running maximums
  • Merge Sorted ArrayEasyTwo pointers from the back
  • 3SumMediumSort + two pointers
  • Next PermutationMediumRightmost ascent, swap, reverse

Get live help in the real interview

Working solutions in real time during your actual interview. Invisible to screensharing.

Start Free Trial

No credit card required

Interview Solver
  • Home
  • About
  • Pricing
  • Sign in
  • Contact
  • Blog
  • Companion
  • Mobile
  • Help Center
  • Developers
  • Use Cases
  • Software Engineer
  • Product Manager
  • Misc
  • Privacy Policy
  • Terms and Conditions
  • Refund & Cancellation
  • Discord Community
  • Affiliate Program
  • Compare
  • All Comparisons
  • vs Final Round AI
  • vs UltraCode
  • vs Interview Coder
  • vs LockedIn AI
  • vs AI Apply
  • Free Tools
  • ATS Resume Checker
  • STAR Method Builder
  • Salary Lookup
  • Big O Cheat Sheet
  • Total Comp Calculator
  • View All →
  • Interview Questions
  • Google Questions
  • Amazon Questions
  • Microsoft Questions
  • Meta Questions
  • Apple Questions
  • TikTok Questions
  • View All →
© 2026 Interview Solver, Inc. All rights reserved.