Challenge - 5 Problems
Stack 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#
using System; using System.Collections.Generic; class Program { static int Trap(int[] height) { int water = 0, current = 0; Stack<int> stack = new Stack<int>(); while (current < height.Length) { while (stack.Count > 0 && height[current] > height[stack.Peek()]) { int top = stack.Pop(); if (stack.Count == 0) break; int distance = current - stack.Peek() - 1; int bounded_height = Math.Min(height[current], height[stack.Peek()]) - height[top]; water += distance * bounded_height; } stack.Push(current); current++; } return water; } static void Main() { int[] height = {0,1,0,2,1,0,1,3,2,1,2,1}; Console.WriteLine(Trap(height)); } }
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 trapped water by measuring the distance between boundaries and the height difference. For the given input, the total trapped water is 6 units.
🧠 Conceptual
intermediateUnderstanding Stack Usage in Trapping Rain Water
Why does the stack-based approach use a stack to solve the trapping rain water problem?
Attempts:
2 left
💡 Hint
Think about how the stack helps identify left and right boundaries for water trapping.
✗ Incorrect
The stack stores indices of bars that form boundaries. When a higher bar is found, it means water can be trapped between the current bar and the bar at the stack's top. This helps calculate trapped water efficiently.
🔧 Debug
advancedIdentify the Error in Trapping Rain Water Code
What error will the following code produce when run?
DSA C#
using System; using System.Collections.Generic; class Program { static int Trap(int[] height) { int water = 0, current = 0; Stack<int> stack = new Stack<int>(); while (current < height.Length) { while (stack.Count > 0 && height[current] >= height[stack.Peek()]) { int top = stack.Pop(); if (stack.Count == 0) break; int distance = current - stack.Peek() - 1; int bounded_height = Math.Min(height[current], height[stack.Peek()]) - height[top]; water += distance * bounded_height; } stack.Push(current); current++; } return water; } static void Main() { int[] height = {0,1,0,2,1,0,1,3,2,1,2,1}; Console.WriteLine(Trap(height)); } }
Attempts:
2 left
💡 Hint
Check the comparison operator in the inner while loop condition.
✗ Incorrect
Using '>=' instead of '>' causes the algorithm to incorrectly calculate trapped water, resulting in an output of 7 instead of 6.
📝 Syntax
advancedSyntax Error in Stack-Based Trapping Rain Water Code
Which option contains a syntax error in the C# code for trapping rain water using a stack?
DSA C#
int Trap(int[] height) { int water = 0, current = 0; Stack<int> stack = new Stack<int>(); while (current < height.Length) { while (stack.Count > 0 && height[current] > height[stack.Peek()]) { int top = stack.Pop() if (stack.Count == 0) break; int distance = current - stack.Peek() - 1; int bounded_height = Math.Min(height[current], height[stack.Peek()]) - height[top]; water += distance * bounded_height; } stack.Push(current); current++; } return water; }
Attempts:
2 left
💡 Hint
Look carefully at the line where a value is popped from the stack.
✗ Incorrect
In C#, each statement must end with a semicolon. The line 'int top = stack.Pop()' is missing a semicolon, causing a syntax error.
🚀 Application
expertMaximum Water Trapped for Large Input
Given the input array representing heights: [5,2,1,2,1,5], what is the total amount of trapped rain water calculated by the stack-based algorithm?
Attempts:
2 left
💡 Hint
Visualize the bars and calculate trapped water between boundaries.
✗ Incorrect
The trapped water is calculated between the bars at indices 0 and 5 with dips in between. The total trapped water is 14 units.
