Container with Most Water Problem Statement

Given a sequence of 'N' space-separated non-negative integers A[1], A[2], ..., A[i], ..., A[n], where each number in the sequence represents the height of a line drawn at point 'i'. On the Cartesian plane, each line is drawn from coordinate ('i', 0) to coordinate ('i', 'A[i]'). The task is to find two lines such that, together with the x-axis, they form a container that can hold the maximum area of water.

Input:
The first line of input contains an integer 'T' representing the number of test cases.
The next '2*T' lines represent the 'T' test cases.
The first line of each test case contains the number of elements in the sequence.
The second line of each test case contains 'n' space-separated integers which is the given sequence.
Output:
For each test case, return the area of the container that can hold the maximum amount of water using any pair of lines from the given sequence.

Example:

Explanation:

In the provided diagram example, suppose the first red marked line is formed between coordinates (2,0) and (2,10), and the second red marked line is formed between coordinates (5,0) and (5,9). The area of water contained is calculated as (height * width) = (5-2) * 9 = 27, which is the maximum area that can be contained between any two lines on the plane. Therefore, the function should return 3* 9 = 27 in this scenario.

Constraints:

  • 1 <= T <= 50
  • 0 <= N <= 10^4
  • 1 <= A[i] <= 10^5
  • Time Limit: 1 second
Note:
1. You cannot slant the container; the height of the water is equal to the minimum height of the two lines that form the container.
2. Do not print anything; simply return the maximum water area the container can hold.
AnswerBot
1y

Given a sequence of non-negative integers representing the height of lines on a cartesian plane, find two lines that form a container with the maximum area of water.

  • Use two pointers approach to find t...read more

Help your peers!
Select
Add answer anonymously...

Amazon Software Developer Intern interview questions & answers

A Software Developer Intern was asked 4mo agoQ. Given a linked list, find the n'th node from the end of the list.
A Software Developer Intern was asked 4mo agoQ. Given a tree, find its diameter (the longest path between two nodes in the tree)...read more
A Software Developer Intern was asked 4mo agoQ. Given a matrix, find the shortest distance between two given points located anyw...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked 4mo agoQ1. Given a linked list, find the n'th node from the end of the list.
A Software Developer Intern was asked 4mo agoQ2. Given a tree, find its diameter (the longest path between two nodes in the tree)...read more
A Software Developer Intern was asked 4mo agoQ3. Given a matrix, find the shortest distance between two given points located anyw...read more
Amazon Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits