Container With Most Water
You are given an integer array `height` of length `n`. There are `n` vertical lines drawn such that the two endpoints of the `i`-th 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. **Examples:** ```typescript maxArea([1,8,6,2,5,4,8,3,7]) // 49 maxArea([1,1]) // 1 ```
Solve It in the BrowserPractice Explaining It Out Loud
Optimal Approach
- Time complexity: O(n)
- Space complexity: O(1)
Two pointers start at boundaries and move inward in a single linear pass.
Follow-Up Questions Interviewers Ask About Container With Most Water
- Why move the pointer with the shorter line inward?
The area is limited by the shorter height. Moving the taller line can only decrease width while height cannot exceed the shorter line, guaranteeing no larger area is missed.
Practice the full problem with execution, tests, and AI code review in the interactive lab — then rehearse explaining your solution in a live voice mock interview.