Challenge - 5 Problems
Container With Most Water Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Container With Most Water Calculation
What is the output of the following code that calculates the maximum water container area?
DSA Python
def max_area(height): left, right = 0, len(height) - 1 max_water = 0 while left < right: width = right - left current_height = min(height[left], height[right]) max_water = max(max_water, width * current_height) if height[left] < height[right]: left += 1 else: right -= 1 return max_water print(max_area([1,8,6,2,5,4,8,3,7]))
Attempts:
2 left
💡 Hint
Think about the width and the minimum height between two lines.
✗ Incorrect
The maximum area is found between the lines at index 1 and 8 with heights 8 and 7, width 7, so area = 7 * 7 = 49.
🧠 Conceptual
intermediate1:30remaining
Understanding the Two Pointer Approach
Why does the two-pointer approach work efficiently for the Container With Most Water problem?
Attempts:
2 left
💡 Hint
Think about why moving the shorter line pointer might help.
✗ Incorrect
The two-pointer method moves inward from both ends, discarding the shorter line because the area is limited by the shorter line, hoping to find a taller line that can increase area.
🔧 Debug
advanced2:00remaining
Identify the Error in Container Area Calculation
What error does the following code raise when executed?
DSA Python
def max_area(height): left, right = 0, len(height) - 1 max_water = 0 while left < right: width = right - left current_height = max(height[left], height[right]) max_water = max(max_water, width * current_height) if height[left] < height[right]: left += 1 else: right -= 1 return max_water print(max_area([1,8,6,2,5,4,8,3,7]))
Attempts:
2 left
💡 Hint
Check how the height is chosen for area calculation.
✗ Incorrect
The code uses max() instead of min() to find the limiting height, which causes it to overestimate the area and return a wrong value.
❓ Predict Output
advanced2:00remaining
Output of Container With Most Water with Equal Heights
What is the output of this code when all heights are equal?
DSA Python
def max_area(height): left, right = 0, len(height) - 1 max_water = 0 while left < right: width = right - left current_height = min(height[left], height[right]) max_water = max(max_water, width * current_height) if height[left] < height[right]: left += 1 else: right -= 1 return max_water print(max_area([5,5,5,5,5]))
Attempts:
2 left
💡 Hint
Calculate area using width and height for equal heights.
✗ Incorrect
The maximum area is between the first and last line: width = 4, height = 5, area = 20.
🚀 Application
expert1:30remaining
Maximum Water Container with Large Input
Given a list of 100,000 heights all set to 1, what is the maximum water container area?
Attempts:
2 left
💡 Hint
Consider the width between the first and last line and the minimum height.
✗ Incorrect
The maximum area is the width between the first and last line (99999) times the height (1), so 99999.