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 TypeScript code that calculates trapped rain water?
DSA Typescript
function trap(height: number[]): number {
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
Think about how water is trapped between bars by comparing left and right max heights.
✗ 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 on that side. The total trapped water for the input is 6 units.
🧠 Conceptual
intermediateUnderstanding the two-pointer approach
In the two-pointer approach to the Trapping Rain Water problem, why do we move the pointer with the smaller height inward?
Attempts:
2 left
💡 Hint
Think about which side limits the water trapped at each step.
✗ Incorrect
Water trapped depends on the smaller boundary. Moving the pointer with smaller height ensures we calculate trapped water correctly using the smaller max height.
🔧 Debug
advancedIdentify the error in this trapping rain water code
What error does the following TypeScript code produce when run?
DSA Typescript
function trap(height: number[]): number {
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 the loop condition carefully and how pointers move.
✗ Incorrect
The loop condition 'left <= right' still works correctly because pointers move inward each iteration. The output is 9 units of trapped water for the input.
❓ Predict Output
advancedOutput of prefix and suffix max arrays method
What is the output of this TypeScript code using prefix and suffix max arrays to calculate trapped rain water?
DSA Typescript
function trap(height: number[]): number {
const n = height.length;
const leftMax = new Array(n).fill(0);
const rightMax = new Array(n).fill(0);
let water = 0;
leftMax[0] = height[0];
for (let i = 1; i < n; i++) {
leftMax[i] = Math.max(leftMax[i - 1], height[i]);
}
rightMax[n - 1] = height[n - 1];
for (let i = n - 2; i >= 0; i--) {
rightMax[i] = Math.max(rightMax[i + 1], height[i]);
}
for (let i = 0; i < n; i++) {
water += Math.min(leftMax[i], rightMax[i]) - height[i];
}
return water;
}
console.log(trap([3,0,2,0,4]));Attempts:
2 left
💡 Hint
Calculate max heights from left and right, then sum trapped water at each index.
✗ Incorrect
The prefix and suffix arrays store max heights from left and right. The trapped water at each index is min(leftMax, rightMax) - height. Total trapped water is 7 units.
🚀 Application
expertMaximum trapped water between two bars
Given an array representing heights of bars, which approach efficiently finds the maximum amount of water trapped between any two bars (not necessarily adjacent)?
Attempts:
2 left
💡 Hint
Think about the container with most water problem approach.
✗ Incorrect
The two-pointer approach moving inward from both ends efficiently finds the max water trapped between two bars by calculating area at each step.
