Challenge - 5 Problems
Trapping Rain Water Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of trapping rain water calculation
What is the output of the following Java code that calculates trapped rain water for the given height array?
DSA Java
public class Solution { public static int trap(int[] height) { int n = height.length; int left = 0, right = n - 1; int leftMax = 0, rightMax = 0; int trapped = 0; while (left < right) { if (height[left] < height[right]) { if (height[left] >= leftMax) leftMax = height[left]; else trapped += leftMax - height[left]; left++; } else { if (height[right] >= rightMax) rightMax = height[right]; else trapped += rightMax - height[right]; right--; } } return trapped; } public static void main(String[] args) { int[] height = {0,1,0,2,1,0,1,3,2,1,2,1}; System.out.println(trap(height)); } }
Attempts:
2 left
💡 Hint
Think about how water is trapped between the tallest bars from left and right.
✗ Incorrect
The code uses two pointers and keeps track of the maximum heights from left and right. It adds trapped water where the current height is less than the max height seen so far. For the given array, the total trapped water units are 6.
❓ Predict Output
intermediateOutput of trapping rain water with uniform heights
What is the output of the following Java code for trapping rain water when all heights are the same?
DSA Java
public class Solution { public static int trap(int[] height) { int n = height.length; int left = 0, right = n - 1; int leftMax = 0, rightMax = 0; int trapped = 0; while (left < right) { if (height[left] < height[right]) { if (height[left] >= leftMax) leftMax = height[left]; else trapped += leftMax - height[left]; left++; } else { if (height[right] >= rightMax) rightMax = height[right]; else trapped += rightMax - height[right]; right--; } } return trapped; } public static void main(String[] args) { int[] height = {3,3,3,3,3}; System.out.println(trap(height)); } }
Attempts:
2 left
💡 Hint
If all bars are the same height, no water can be trapped.
✗ Incorrect
Since all bars have the same height, there is no space to trap water between them. The output is 0.
🔧 Debug
advancedIdentify the error in this trapping rain water code
What error will this Java code produce when run?
DSA Java
public class Solution { public static int trap(int[] height) { int n = height.length; int left = 0, right = n - 1; int leftMax = 0, rightMax = 0; int trapped = 0; while (left <= right) { if (height[left] < height[right]) { if (height[left] > leftMax) leftMax = height[left]; else trapped += leftMax - height[left]; left++; } else { if (height[right] > rightMax) rightMax = height[right]; else trapped += rightMax - height[right]; right--; } } return trapped; } public static void main(String[] args) { int[] height = {0,1,0,2,1,0,1,3,2,1,2,1}; System.out.println(trap(height)); } }
Attempts:
2 left
💡 Hint
Check the loop condition and how it affects counting trapped water.
✗ Incorrect
The loop condition uses <= instead of <, causing the middle element to be processed twice, leading to an overcount of trapped water by 2 units. The output is 8 instead of 6.
🧠 Conceptual
advancedUnderstanding the two-pointer approach in trapping rain water
Why does the two-pointer approach work correctly for the trapping rain water problem?
Attempts:
2 left
💡 Hint
Think about how water trapping depends on the smaller boundary.
✗ Incorrect
The two-pointer approach works by moving pointers inward from both ends, always advancing the pointer with the smaller height. This ensures trapped water is counted correctly based on the smaller max height boundary.
🚀 Application
expertMaximum trapped water for a custom height array
Given the height array {4,2,0,3,2,5}, what is the total amount of trapped rain water?
Attempts:
2 left
💡 Hint
Visualize the bars and count water trapped between them carefully.
✗ Incorrect
The trapped water units total 9: position 1 (2 units), position 2 (4 units), position 3 (1 unit), position 4 (2 units).
