Challenge - 5 Problems
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 JavaScript code that calculates trapped rain water for the given height array?
DSA Javascript
function trap(height) {
let left = 0, right = height.length - 1;
let leftMax = 0, rightMax = 0;
let water = 0;
while (left < right) {
if (height[left] < height[right]) {
if (height[left] >= leftMax) leftMax = height[left];
else water += leftMax - height[left];
left++;
} else {
if (height[right] >= rightMax) rightMax = height[right];
else water += rightMax - height[right];
right--;
}
}
return water;
}
console.log(trap([0,1,0,2,1,0,1,3,2,1,2,1]));Attempts:
2 left
💡 Hint
Trace the water trapped between bars by comparing left and right max heights.
✗ Incorrect
The code uses two pointers moving inward, tracking the max heights on both sides to calculate trapped water correctly. The total trapped water for the input array is 6 units.
🧠 Conceptual
intermediateUnderstanding the role of leftMax and rightMax
In the trapping rain water problem, what do the variables leftMax and rightMax represent during the calculation?
Attempts:
2 left
💡 Hint
Think about how the algorithm decides how much water can be trapped at each position.
✗ Incorrect
leftMax and rightMax track the tallest bars encountered from the left and right sides to determine the water level at each pointer.
❓ Predict Output
advancedOutput of modified trapping rain water code with an extra condition
What is the output of this modified trapping rain water code snippet?
DSA Javascript
function trap(height) {
let left = 0, right = height.length - 1;
let leftMax = 0, rightMax = 0;
let water = 0;
while (left <= right) {
if (height[left] < height[right]) {
if (height[left] > leftMax) leftMax = height[left];
else water += leftMax - height[left];
left++;
} else {
if (height[right] > rightMax) rightMax = height[right];
else water += rightMax - height[right];
right--;
}
}
return water;
}
console.log(trap([4,2,0,3,2,5]));Attempts:
2 left
💡 Hint
Check how the <= condition affects the loop and the water calculation.
✗ Incorrect
The code correctly calculates trapped water as 9 units for the input array, including the last position due to <= condition.
🔧 Debug
advancedIdentify the error in this trapping rain water code
What error does this code produce when run with input [3,0,2,0,4]?
DSA Javascript
function trap(height) {
let left = 0, right = height.length - 1;
let leftMax = 0, rightMax = 0;
let water = 0;
while (left < right) {
if (height[left] <= height[right]) {
if (height[left] >= leftMax) leftMax = height[left];
else water += leftMax - height[left];
left++;
} else {
if (height[right] >= rightMax) rightMax = height[right];
else water += rightMax - height[right];
right--;
}
}
return water;
}
console.log(trap([3,0,2,0,4]));Attempts:
2 left
💡 Hint
Trace the water trapped step by step with the given input.
✗ Incorrect
The code outputs 7 units of trapped water for the input array [3,0,2,0,4].
🧠 Conceptual
expertWhy does the two-pointer approach work for trapping rain water?
Why is the two-pointer approach efficient and correct for solving the trapping rain water problem?
Attempts:
2 left
💡 Hint
Think about how the algorithm avoids using extra arrays for max heights.
✗ Incorrect
The two-pointer approach efficiently calculates trapped water by moving pointers inward and updating max heights from both ends, avoiding extra space.
