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 Container With Most Water Calculation
What is the output of the following Java code that calculates the maximum water container area?
DSA Java
public class Solution { public static int maxArea(int[] height) { int maxArea = 0; int left = 0, right = height.length - 1; while (left < right) { int width = right - left; int currentArea = Math.min(height[left], height[right]) * width; maxArea = Math.max(maxArea, currentArea); if (height[left] < height[right]) { left++; } else { right--; } } return maxArea; } public static void main(String[] args) { int[] height = {1,8,6,2,5,4,8,3,7}; System.out.println(maxArea(height)); } }
Attempts:
2 left
💡 Hint
Think about the two-pointer approach and how the area is calculated by the shorter line times the distance.
✗ Incorrect
The maximum area is 49, formed between the lines at index 1 (height 8) and index 8 (height 7). The width is 7, so area = 7 * 7 = 49.
🧠 Conceptual
intermediateUnderstanding the Two-Pointer Approach
Why does the two-pointer approach work efficiently for the Container With Most Water problem?
Attempts:
2 left
💡 Hint
Consider what limits the area between two lines.
✗ Incorrect
The area is limited by the shorter line, so moving the pointer at the smaller height may find a taller line and increase the area.
🔧 Debug
advancedIdentify the Error in Container With Most Water Code
What error will this Java code produce when run?
DSA Java
public class Solution { public static int maxArea(int[] height) { int maxArea = 0; int left = 0, right = height.length; while (left < right) { int width = right - left; int currentArea = Math.min(height[left], height[right]) * width; maxArea = Math.max(maxArea, currentArea); if (height[left] < height[right]) { left++; } else { right--; } } return maxArea; } public static void main(String[] args) { int[] height = {1,8,6,2,5,4,8,3,7}; System.out.println(maxArea(height)); } }
Attempts:
2 left
💡 Hint
Check the initialization of the right pointer and array indexing.
✗ Incorrect
The right pointer is initialized to height.length, which is out of bounds for array indexing (max index is length-1), causing ArrayIndexOutOfBoundsException.
🚀 Application
advancedMaximum Water Container for Custom Input
Given the array heights = {4,3,2,1,4}, what is the maximum water container area?
Attempts:
2 left
💡 Hint
Calculate area between pairs and find the maximum.
✗ Incorrect
The maximum area is between the first and last lines (index 0 and 4), min height 4, width 4, area = 16.
❓ Predict Output
expertOutput of Modified Container With Most Water Code with Equal Heights
What is the output of this Java code?
DSA Java
public class Solution { public static int maxArea(int[] height) { int maxArea = 0; int left = 0, right = height.length - 1; while (left < right) { int width = right - left; int currentArea = Math.min(height[left], height[right]) * width; maxArea = Math.max(maxArea, currentArea); if (height[left] <= height[right]) { left++; } else { right--; } } return maxArea; } public static void main(String[] args) { int[] height = {5,5,5,5,5}; System.out.println(maxArea(height)); } }
Attempts:
2 left
💡 Hint
All heights are equal; area depends on width.
✗ Incorrect
The max area is between index 0 and 4, height 5, width 4, area = 20.
