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 using a stack?
DSA C++
#include <iostream> #include <vector> #include <stack> using namespace std; int trap(vector<int>& height) { int n = height.size(); stack<int> st; int water = 0; for (int i = 0; i < n; i++) { while (!st.empty() && height[i] > height[st.top()]) { int top = st.top(); st.pop(); if (st.empty()) break; int distance = i - st.top() - 1; int bounded_height = min(height[i], height[st.top()]) - height[top]; water += distance * bounded_height; } st.push(i); } return water; } int main() { vector<int> height = {0,1,0,2,1,0,1,3,2,1,2,1}; cout << trap(height) << endl; return 0; }
Attempts:
2 left
💡 Hint
Think about how the stack helps find boundaries to trap water between bars.
✗ Incorrect
The code uses a stack to find the left and right boundaries for trapped water. It calculates the water trapped between bars by considering the distance and bounded height. For the given input, the total trapped water is 6 units.
🧠 Conceptual
intermediateUnderstanding Stack Usage in Trapping Rain Water
Why does the stack help in solving the trapping rain water problem efficiently?
Attempts:
2 left
💡 Hint
Think about how boundaries are found between bars.
✗ Incorrect
The stack stores indices of bars in a way that helps find left and right boundaries for water trapping. When a higher bar is found, it calculates trapped water between the current bar and the bar at the stack's top.
🔧 Debug
advancedIdentify the Error in Trapping Rain Water Code
What error will the following code produce when run?
DSA C++
#include <iostream> #include <vector> #include <stack> using namespace std; int trap(vector<int>& height) { int n = height.size(); stack<int> st; int water = 0; for (int i = 0; i < n; i++) { while (!st.empty() && height[i] > height[st.top()]) { int top = st.top(); st.pop(); if (st.empty()) break; int distance = i - st.top() - 1; int bounded_height = min(height[i], height[st.top()]) - height[top]; water += distance * bounded_height; } st.push(i); } return water; } int main() { vector<int> height = {4,2,0,3,2,5}; cout << trap(height) << endl; return 0; }
Attempts:
2 left
💡 Hint
Check the loop boundary condition carefully.
✗ Incorrect
The original code had 'for (int i = 0; i <= n; i++)' which accesses height[n], causing out_of_range runtime error. It should be 'for (int i = 0; i < n; i++)'.
❓ Predict Output
advancedOutput of Modified Trapping Rain Water Code
What is the output of this modified C++ code snippet?
DSA C++
#include <iostream> #include <vector> #include <stack> using namespace std; int trap(vector<int>& height) { int n = height.size(); stack<int> st; int water = 0; for (int i = 0; i < n; i++) { while (!st.empty() && height[i] >= height[st.top()]) { int top = st.top(); st.pop(); if (st.empty()) break; int distance = i - st.top() - 1; int bounded_height = min(height[i], height[st.top()]) - height[top]; water += distance * bounded_height; } st.push(i); } return water; } int main() { vector<int> height = {4,2,0,3,2,5}; cout << trap(height) << endl; return 0; }
Attempts:
2 left
💡 Hint
Note the use of '>=' in the while condition instead of '>'.
✗ Incorrect
Using '>=' allows popping bars of equal height, but the trapped water calculation remains the same for this input, resulting in 9 units of water.
🚀 Application
expertMaximum Water Trapped in a Large Histogram
Given a histogram with heights: [5,2,1,2,1,5,3,2,4,1,2,3,1,2,1], what is the maximum amount of water trapped using the stack method?
Attempts:
2 left
💡 Hint
Visualize the bars and calculate trapped water between boundaries using the stack approach.
✗ Incorrect
The stack method calculates trapped water by finding boundaries and summing trapped units. For this input, the total trapped water is 28 units.
