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 C# code that calculates trapped rain water for the given height array?
DSA C#
int[] height = {0,1,0,2,1,0,1,3,2,1,2,1}; int trappedWater = 0; int left = 0, right = height.Length - 1; int leftMax = 0, rightMax = 0; while (left < right) { if (height[left] < height[right]) { if (height[left] >= leftMax) leftMax = height[left]; else trappedWater += leftMax - height[left]; left++; } else { if (height[right] >= rightMax) rightMax = height[right]; else trappedWater += rightMax - height[right]; right--; } } System.Console.WriteLine(trappedWater);
Attempts:
2 left
💡 Hint
Think about how water is trapped between the bars by comparing left and right pointers.
✗ 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 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
Consider which side limits the water level at each step.
✗ Incorrect
Water trapped depends on the smaller boundary between left and right. Moving the pointer with smaller height helps find trapped water correctly by updating max heights and calculating trapped water.
🔧 Debug
advancedIdentify the Error in Trapping Rain Water Code
What error will the following C# code produce when executed?
DSA C#
int[] height = {4,2,0,3,2,5}; int trappedWater = 0; int left = 0, right = height.Length - 1; int leftMax = 0, rightMax = 0; while (left <= right) { if (height[left] < height[right]) { if (height[left] > leftMax) leftMax = height[left]; else trappedWater += leftMax - height[left]; left++; } else { if (height[right] > rightMax) rightMax = height[right]; else trappedWater += rightMax - height[right]; right--; } } System.Console.WriteLine(trappedWater);
Attempts:
2 left
💡 Hint
Check the loop condition and pointer updates carefully.
✗ Incorrect
The loop condition uses <= which causes left and right pointers to cross and the loop never ends because pointers are updated only inside conditions that may not always move them correctly.
❓ Predict Output
advancedOutput of Prefix and Suffix Max Arrays
What is the output of the following C# code that computes prefix and suffix max arrays for trapping rain water?
DSA C#
int[] height = {3,0,2,0,4}; int n = height.Length; int[] leftMax = new int[n]; int[] rightMax = new int[n]; leftMax[0] = height[0]; for (int i = 1; i < n; i++) { leftMax[i] = Math.Max(leftMax[i - 1], height[i]); } rightMax[n - 1] = height[n - 1]; for (int i = n - 2; i >= 0; i--) { rightMax[i] = Math.Max(rightMax[i + 1], height[i]); } System.Console.WriteLine(string.Join(",", leftMax)); System.Console.WriteLine(string.Join(",", rightMax));
Attempts:
2 left
💡 Hint
Prefix max array stores max from left to current index, suffix max from right to current index.
✗ Incorrect
leftMax array stores max height from left up to each index: [3,3,3,3,4]. rightMax stores max height from right up to each index: [4,4,4,4,4].
🚀 Application
expertCalculate Total Trapped Water for Custom Input
Given the height array {5,2,1,2,1,5}, what is the total amount of trapped rain water calculated by the standard trapping rain water algorithm?
Attempts:
2 left
💡 Hint
Visualize the bars and calculate trapped water between the boundaries.
✗ Incorrect
The trapped water units are 3 between 5 and 2, 1 between 2 and 1, 2 between 1 and 2, 1 between 2 and 1, and 5 between 1 and 5, totaling 12 units.
