Most Water Between Two Lines
Updated
Problem statement
You are given an integer array height. Picture height.length vertical lines standing on a flat floor, one at each index. The line at index i has height height[i].
Choose two different lines. Together with the floor they form a container. The water inside can rise only as high as the shorter of the two lines, and its width is the distance between the two indexes. So lines i and j (with i < j) hold min(height[i], height[j]) * (j - i) units of water. The lines between them do not block the water.
Return the largest amount of water that any pair of lines can hold.
Function
maxArea(height: int[]) → intExamples
Example 1
height = [2,7,3,8,4,1,6]return = 30Use the lines at index 1 (height 7) and index 6 (height 6). They are 6 - 1 = 5 apart, and the water can only rise to the shorter line, 6, so the area is 6 * 5 = 30. The two tallest lines, at index 1 and index 3, hold only 7 * 2 = 14 because they are close together.
Example 2
height = [5,5]return = 5There is only one pair. Both lines have height 5 and they are 1 apart, so the area is 5 * 1 = 5.
Example 3
height = [1,9,9,1]return = 9The outer lines are 3 apart but only 1 tall: 1 * 3 = 3. The two middle lines are 1 apart and 9 tall: 9 * 1 = 9, which is the best.
Constraints
2 <= height.length <= 10^40 <= height[i] <= 10^4
More Expedia Onsite Interview questions
- Scramble StringONSITE INTERVIEW · Seen Sep 2026
- Smallest Sufficient TeamONSITE INTERVIEW · Seen Sep 2026
- Check Anagram Pairs Index by IndexONSITE INTERVIEW · Seen Oct 2025
- Fresh Oranges Left After Rotting StopsONSITE INTERVIEW · Seen Oct 2025
- Longest Increasing SubsequenceONSITE INTERVIEW · Seen Oct 2025
- Lowest Common Ancestor with Repeated and Missing ValuesONSITE INTERVIEW · Seen Oct 2025
- Shortest Substring with Exactly K OnesONSITE INTERVIEW · Seen Oct 2025
- Middle Element of a Linked ListONSITE INTERVIEW · Seen Nov 2024
- Minimum in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Sep 2024
- Mirror Image Binary TreesONSITE INTERVIEW · Seen Sep 2024
Similar questions at other companies
- Minimum Station Tracks for a Train ScheduleBNSF Railway · ONSITE INTERVIEWBNSF Railway interview questions
- Container With Most WaterAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Assign Cookies With Matching ParityInfosys · ONSITE INTERVIEWInfosys interview questions
- Container With Most WatereBay · ONSITE INTERVIEWeBay interview questions
- Rebalance Bank Accounts to a Minimum BalanceStripe · ONSITE INTERVIEWStripe interview questions
- Maximum Easy Tasks After RearrangingIBM · OAIBM interview questions
- Container With Most WaterEPAM · PHONE SCREENEPAM interview questions
- Latest Arrival to Catch a BusAutodesk · OAAutodesk interview questions
- Container With Most WaterNuro · PHONE SCREENNuro interview questions
- Minimum Last-Element RelocationsChubb · OAChubb interview questions
Expedia interview experiences
- Expedia Intern Final Round in 2021: 28 Experiences (Part 4 of 6)
- Expedia Intern Finals Before 2020: 14 Experiences (Part 6 of 6)
- Expedia Intern Final Round in 2020: 24 Experiences (Part 5 of 6)
- Expedia Intern Final Round in 2022: 15 Experiences (Part 3 of 6)
- Expedia Intern Final Round in 2023: 17 Experiences (Part 2 of 6)
- Expedia SWE Intern Final Round: 126 Experiences (Part 1 of 6)