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 trap(std::vector<int>& height) { int n = height.size(); int left = 0, right = n - 1; int left_max = 0, right_max = 0; int water = 0; while (left < right) { if (height[left] < height[right]) { if (height[left] >= left_max) left_max = height[left]; else water += left_max - height[left]; left++; } else { if (height[right] >= right_max) right_max = height[right]; else water += right_max - height[right]; right--; } } return water; } int main() { std::vector<int> height = {0,1,0,2,1,0,1,3,2,1,2,1}; std::cout << trap(height); return 0; }
Attempts:
2 left
💡 Hint
Use two pointers and track the maximum heights from both ends to calculate trapped water.
✗ Incorrect
The code uses two pointers from left and right ends, updating left_max and right_max. Water trapped is the sum of differences between max heights and current heights where applicable. For the given array, total trapped water is 6 units.
🧠 Conceptual
intermediateUnderstanding the trapping rain water problem
Which of the following best describes the main idea behind the trapping rain water problem?
Attempts:
2 left
💡 Hint
Think about how water is trapped between taller bars on both sides.
✗ Incorrect
Water trapped at each position depends on the shorter of the tallest bars to the left and right minus the height at that position.
🔧 Debug
advancedIdentify the error in the trapping rain water code
What error will the following C++ code produce when run?
DSA C++
int trap(std::vector<int>& height) { int n = height.size(); int left = 0, right = n - 1; int left_max = 0, right_max = 0; int water = 0; while (left <= right) { if (height[left] < height[right]) { if (height[left] >= left_max) left_max = height[left]; else water += left_max - height[left]; left++; } else { if (height[right] >= right_max) right_max = height[right]; else water += right_max - height[right]; right--; } } return water; } int main() { std::vector<int> height = {4,2,0,3,2,5}; std::cout << trap(height); return 0; }
Attempts:
2 left
💡 Hint
Check the loop condition and pointer updates carefully.
✗ Incorrect
There is no error in the code. The loop condition left <= right does not cause an infinite loop because when left == right, the else branch is executed (since height[left] == height[right]), updating right_max and decrementing right, causing left > right and the loop to exit. The code correctly outputs 9.
🚀 Application
advancedCalculate trapped water for a custom height array
Given the height array [3, 0, 2, 0, 4], what is the total trapped rain water?
Attempts:
2 left
💡 Hint
Visualize the bars and count water units between them.
✗ Incorrect
Water trapped: at index 1: min(3,4) - 0 = 3, at index 2: min(3,4) - 2 = 1, at index 3: min(3,4) - 0 = 3, totaling 7 units.
❓ Predict Output
expertOutput of optimized trapping rain water code with prefix and suffix arrays
What is the output of the following C++ code that uses prefix and suffix arrays to calculate trapped rain water?
DSA C++
#include <iostream> #include <vector> #include <algorithm> int trap(std::vector<int>& height) { int n = height.size(); if (n == 0) return 0; std::vector<int> left_max(n), right_max(n); left_max[0] = height[0]; for (int i = 1; i < n; i++) { left_max[i] = std::max(left_max[i-1], height[i]); } right_max[n-1] = height[n-1]; for (int i = n-2; i >= 0; i--) { right_max[i] = std::max(right_max[i+1], height[i]); } int water = 0; for (int i = 0; i < n; i++) { water += std::min(left_max[i], right_max[i]) - height[i]; } return water; } int main() { std::vector<int> height = {4,2,0,3,2,5}; std::cout << trap(height); return 0; }
Attempts:
2 left
💡 Hint
Use prefix and suffix max arrays to find trapped water at each position.
✗ Incorrect
The prefix and suffix arrays store max heights from left and right. The trapped water is sum of min(left_max, right_max) - height at each index. For the input, total trapped water is 9.
