โ† Core Algorithms
#10 most askedVery commonMediumTwo pointers

Container With Most Water

New to Two pointers? Learn it from scratch first
Two Pointers & Stacks โ†’
๐Ÿข Asked in real interviews
No reports yet. Were you asked this? Be the first to say where.
I was asked this

height[i] is the height of a vertical line at position i. Pick two lines that, together with the x-axis, hold the most water. Return that amount.

[1, 8, 6, 2, 5, 4, 8, 3, 7] โ†’ 49 (lines at 1 and 8: width 7 ร— height 7).

๐Ÿงช Practice

Write your solution, then press Run tests. Explain your approach out loud as you go.

Python
def max_area(height):
    pass

Output appears here. Press Run or Ctrl/โŒ˜ + Enter.

Try it first. Struggling for a few minutes is how it sticks.