Bird
0
0
DSA Cprogramming~20 mins

Trapping Rain Water Using Stack in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Stack Rain Water Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Trapping Rain Water Calculation
What is the output of the following C code that calculates trapped rain water using a stack?
DSA C
int trap(int* height, int heightSize) {
    int stack[heightSize];
    int top = -1;
    int water = 0;
    int i = 0;
    while (i < heightSize) {
        while (top != -1 && height[i] > height[stack[top]]) {
            int topIndex = stack[top--];
            if (top == -1) break;
            int distance = i - stack[top] - 1;
            int bounded_height = (height[i] < height[stack[top]] ? height[i] : height[stack[top]]) - height[topIndex];
            water += distance * bounded_height;
        }
        stack[++top] = i++;
    }
    return water;
}

int main() {
    int height[] = {0,1,0,2,1,0,1,3,2,1,2,1};
    int size = sizeof(height)/sizeof(height[0]);
    int result = trap(height, size);
    printf("%d\n", result);
    return 0;
}
A7
B5
C6
D8
Attempts:
2 left
💡 Hint
Trace the stack operations and calculate trapped water between bars.
Predict Output
intermediate
2:00remaining
Result of Trapping Rain Water for Different Input
What is the output of the trapping rain water function for this input array?
DSA C
int height[] = {4,2,0,3,2,5};
int size = sizeof(height)/sizeof(height[0]);
int result = trap(height, size);
printf("%d\n", result);
A9
B8
C7
D6
Attempts:
2 left
💡 Hint
Calculate trapped water between bars using stack logic.
🔧 Debug
advanced
2:00remaining
Identify the Error in Stack Implementation
What error will occur if the stack array is declared as int stack[heightSize-1] instead of int stack[heightSize] in the trapping rain water code?
DSA C
int stack[heightSize-1];
int top = -1;
// rest of the code unchanged
ANo error, code runs fine
BBuffer overflow when pushing last element
CStack underflow error during pop
DCompilation error due to invalid array size
Attempts:
2 left
💡 Hint
Consider the maximum number of elements pushed onto the stack.
🧠 Conceptual
advanced
2:00remaining
Why Use Stack for Trapping Rain Water?
Why is a stack used in the trapping rain water problem instead of a simple two-pointer approach?
AStack helps track previous bars to calculate bounded water efficiently
BStack reduces time complexity from O(n) to O(log n)
CStack automatically sorts the bars for easier calculation
DStack avoids the need to traverse the array multiple times
Attempts:
2 left
💡 Hint
Think about how the stack stores indices of bars and helps find boundaries.
🚀 Application
expert
3:00remaining
Maximum Water Trapped with Modified Input
Given the input array {5,2,1,2,1,5}, what is the maximum amount of water trapped calculated by the stack method?
A12
B15
C13
D14
Attempts:
2 left
💡 Hint
Visualize the bars and calculate trapped water between boundaries.