Skip to content
CodeKruPro

Container with the most water

Coding problemMediumAsked in: Amazon, Flipkart, Google

You are given a list of non-negative integers. Value heights[i] is the height of a vertical line standing at position i. Two lines and the ground between them form a container. The water it holds is the distance between the two positions times the height of the shorter line.

Return the largest amount of water any two lines can hold. With fewer than two lines the answer is 0.

Examples

Example 1
Input: heights = [1,8,6,2,5,4,8,3,7]
Output: 49
Lines at positions 1 and 8 have heights 8 and 7. The distance is 7, so the water is 7 x 7 = 49.
Example 2
Input: heights = [1,1]
Output: 1
Example 3
Input: heights = [5]
Output: 0

Constraints

  • The list holds between 0 and 100,000 values
  • Each height is between 0 and 10,000

Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.

Run your code to see the results here.

This question is part of Pro+

Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.

See Pro+

Already have Pro+? Sign in