Challenge - 5 Problems
Container With Most Water Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of max water container calculation
What is the output of the following Go code that calculates the maximum water container area?
DSA Go
package main import "fmt" func maxArea(height []int) int { left, right := 0, len(height)-1 maxArea := 0 for left < right { width := right - left var h int if height[left] < height[right] { h = height[left] left++ } else { h = height[right] right-- } area := width * h if area > maxArea { maxArea = area } } return maxArea } func main() { heights := []int{1,8,6,2,5,4,8,3,7} fmt.Println(maxArea(heights)) }
Attempts:
2 left
💡 Hint
Consider the two-pointer approach moving inward and calculating area at each step.
✗ Incorrect
The maximum area is found between the lines at index 1 (height 8) and index 8 (height 7), width 7, area 7*7=49.
🧠 Conceptual
intermediateUnderstanding the two-pointer technique in Container With Most Water
Why does the two-pointer technique work efficiently for the Container With Most Water problem?
Attempts:
2 left
💡 Hint
Think about how the height limits the area and which pointer to move.
✗ Incorrect
The pointer with smaller height limits the area, so moving it inward may find a taller line and possibly a larger area.
❓ Predict Output
advancedOutput of modified container water calculation with equal heights
What is the output of this Go code snippet that calculates max water container area with equal heights?
DSA Go
package main import "fmt" func maxArea(height []int) int { left, right := 0, len(height)-1 maxArea := 0 for left < right { width := right - left var h int if height[left] <= height[right] { h = height[left] left++ } else { h = height[right] right-- } area := width * h if area > maxArea { maxArea = area } } return maxArea } func main() { heights := []int{5,5,5,5,5} fmt.Println(maxArea(heights)) }
Attempts:
2 left
💡 Hint
All heights are equal, so area depends on width.
✗ Incorrect
The maximum area is height 5 times width 4 (between index 0 and 4), so 20.
🔧 Debug
advancedIdentify the error in this Container With Most Water implementation
What error does this Go code produce when run?
DSA Go
package main import "fmt" func maxArea(height []int) int { left, right := 0, len(height)-1 maxArea := 0 for left <= right { width := right - left var h int if height[left] < height[right] { h = height[left] left++ } else { h = height[right] right-- } area := width * h if area > maxArea { maxArea = area } } return maxArea } func main() { heights := []int{1,2,3,4,5} fmt.Println(maxArea(heights)) }
Attempts:
2 left
💡 Hint
Check the loop condition and pointer movement.
✗ Incorrect
The loop condition allows left == right, but pointers move only when left < right, causing infinite loop when left == right.
🚀 Application
expertMaximum water container with additional constraint
Given an array of heights, find the maximum water container area where the container width must be an even number. Which option correctly implements this constraint in Go?
Attempts:
2 left
💡 Hint
Calculate area only when width is even, but always move pointers to avoid infinite loops.
✗ Incorrect
Option A calculates area only if width is even but moves pointers every iteration, ensuring progress and correct max area calculation.
