Challenge - 5 Problems
Stack Rainwater Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of trapping rain water calculation with given heights
What is the output of the following Go code that calculates trapped rain water using a stack?
DSA Go
package main import "fmt" func trap(height []int) int { stack := []int{} water := 0 for i := 0; i < len(height); i++ { for len(stack) > 0 && height[i] > height[stack[len(stack)-1]] { top := stack[len(stack)-1] stack = stack[:len(stack)-1] if len(stack) == 0 { break } distance := i - stack[len(stack)-1] - 1 boundedHeight := min(height[i], height[stack[len(stack)-1]]) - height[top] water += distance * boundedHeight } stack = append(stack, i) } return water } func min(a, b int) int { if a < b { return a } return b } func main() { heights := []int{0,1,0,2,1,0,1,3,2,1,2,1} fmt.Println(trap(heights)) }
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 finding the distance between boundaries and the height difference. For the given heights, the total trapped water is 6 units.
❓ Predict Output
intermediateOutput of trapping rain water with uniform height bars
What is the output of this Go code that calculates trapped rain water using a stack for uniform height bars?
DSA Go
package main import "fmt" func trap(height []int) int { stack := []int{} water := 0 for i := 0; i < len(height); i++ { for len(stack) > 0 && height[i] > height[stack[len(stack)-1]] { top := stack[len(stack)-1] stack = stack[:len(stack)-1] if len(stack) == 0 { break } distance := i - stack[len(stack)-1] - 1 boundedHeight := min(height[i], height[stack[len(stack)-1]]) - height[top] water += distance * boundedHeight } stack = append(stack, i) } return water } func min(a, b int) int { if a < b { return a } return b } func main() { heights := []int{3,3,3,3,3} fmt.Println(trap(heights)) }
Attempts:
2 left
💡 Hint
Uniform height bars cannot trap water between them.
✗ Incorrect
Since all bars have the same height, no water can be trapped between them. The output is 0.
🔧 Debug
advancedIdentify the runtime error in trapping rain water code
What runtime error will this Go code produce when calculating trapped rain water using a stack?
DSA Go
package main import "fmt" func trap(height []int) int { stack := []int{} water := 0 for i := 0; i <= len(height); i++ { for len(stack) > 0 && height[i] > height[stack[len(stack)-1]] { top := stack[len(stack)-1] stack = stack[:len(stack)-1] if len(stack) == 0 { break } distance := i - stack[len(stack)-1] - 1 boundedHeight := min(height[i], height[stack[len(stack)-1]]) - height[top] water += distance * boundedHeight } stack = append(stack, i) } return water } func min(a, b int) int { if a < b { return a } return b } func main() { heights := []int{0,1,0,2,1,0,1,3,2,1,2,1} fmt.Println(trap(heights)) }
Attempts:
2 left
💡 Hint
Check the loop condition and array indexing carefully.
✗ Incorrect
The loop runs with i <= len(height), which causes an index out of range error when i equals len(height).
🧠 Conceptual
advancedWhy does the stack help in trapping rain water problem?
Which of the following best explains why a stack is used in the trapping rain water algorithm?
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 to calculate trapped water between them efficiently.
❓ Predict Output
expertOutput of trapping rain water with complex height pattern
What is the output of this Go code that calculates trapped rain water using a stack for a complex height pattern?
DSA Go
package main import "fmt" func trap(height []int) int { stack := []int{} water := 0 for i := 0; i < len(height); i++ { for len(stack) > 0 && height[i] > height[stack[len(stack)-1]] { top := stack[len(stack)-1] stack = stack[:len(stack)-1] if len(stack) == 0 { break } distance := i - stack[len(stack)-1] - 1 boundedHeight := min(height[i], height[stack[len(stack)-1]]) - height[top] water += distance * boundedHeight } stack = append(stack, i) } return water } func min(a, b int) int { if a < b { return a } return b } func main() { heights := []int{4,2,0,3,2,5} fmt.Println(trap(heights)) }
Attempts:
2 left
💡 Hint
Visualize the bars and trapped water between them carefully.
✗ Incorrect
The total trapped water for the given heights is 9 units, calculated by summing trapped water between boundaries found using the stack.
