Skip to content
MediumArraysAI interview only

Container With Most Water

Asked atamazongooglemetabloombergadobe

01 · 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.

Notice that you may not slant the container.

02 · Examples

Example 01
Input
height = [1,8,6,2,5,4,8,3,7]
Output
49

Using lines at index 1 (height 8) and index 8 (height 7): area = min(8, 7) * (8 - 1) = 7 * 7 = 49.

Example 02
Input
height = [1,1]
Output
1

Only two lines available. Area = min(1, 1) * (1 - 0) = 1.

03 · Constraints

  • 01n == height.length
  • 022 <= n <= 105
  • 030 <= height[i] <= 104

04 · Optimal complexity

Time
O(n)
Space
O(1)
05 · Two ways to work on it

Practice it alone or rehearse it as an interview.

Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.