Bird
0
0
DSA Cprogramming~20 mins

Trapping Rain Water Problem in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
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 using two-pointer approach
What is the output of the following C code that calculates trapped rain water using the two-pointer technique?
DSA C
#include <stdio.h>

int trap(int* height, int heightSize) {
    int left = 0, right = heightSize - 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() {
    int height[] = {0,1,0,2,1,0,1,3,2,1,2,1};
    int size = sizeof(height)/sizeof(height[0]);
    printf("%d\n", trap(height, size));
    return 0;
}
A6
B5
C7
D8
Attempts:
2 left
💡 Hint
Think about how the two-pointer approach accumulates water by comparing left and right heights.
🧠 Conceptual
intermediate
1:30remaining
Understanding the role of max height arrays in trapping rain water
In the trapping rain water problem, why do we precompute arrays of maximum heights to the left and right of each position?
ATo know the highest wall on both sides to calculate trapped water at each position
BTo sort the heights for easier calculation
CTo find the minimum height in the array
DTo count the number of walls taller than current position
Attempts:
2 left
💡 Hint
Think about how trapped water depends on the tallest walls around a position.
🔧 Debug
advanced
2:00remaining
Identify the error in this trapping rain water implementation
What error does the following C code produce when run?
DSA C
#include <stdio.h>

int trap(int* height, int heightSize) {
    int left_max[heightSize];
    int right_max[heightSize];
    int water = 0;
    left_max[0] = height[0];
    for (int i = 1; i < heightSize; i++) {
        left_max[i] = (height[i] > left_max[i-1]) ? height[i] : left_max[i-1];
    }
    right_max[heightSize] = height[heightSize-1];
    for (int i = heightSize - 2; i >= 0; i--) {
        right_max[i] = (height[i] > right_max[i+1]) ? height[i] : right_max[i+1];
    }
    for (int i = 0; i < heightSize; i++) {
        water += (left_max[i] < right_max[i] ? left_max[i] : right_max[i]) - height[i];
    }
    return water;
}

int main() {
    int height[] = {4,2,0,3,2,5};
    int size = sizeof(height)/sizeof(height[0]);
    printf("%d\n", trap(height, size));
    return 0;
}
ADivision by zero error
BArray index out of bounds error at right_max[heightSize]
CNo error, outputs 9
DSegmentation fault due to null pointer
Attempts:
2 left
💡 Hint
Check array indexing carefully, especially at boundaries.
Predict Output
advanced
2:00remaining
Output of trapping rain water using stack approach
What is the output of this C code that calculates trapped rain water using a stack?
DSA C
#include <stdio.h>
#include <stdlib.h>

int trap(int* height, int heightSize) {
    int *stack = (int*)malloc(heightSize * sizeof(int));
    int top = -1;
    int water = 0;
    for (int i = 0; i < heightSize; i++) {
        while (top != -1 && height[i] > height[stack[top]]) {
            int top_height = height[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]]) - top_height;
            water += distance * bounded_height;
        }
        stack[++top] = i;
    }
    free(stack);
    return water;
}

int main() {
    int height[] = {0,2,0,3,0,1,0,4};
    int size = sizeof(height)/sizeof(height[0]);
    printf("%d\n", trap(height, size));
    return 0;
}
A9
B7
C8
D10
Attempts:
2 left
💡 Hint
Stack stores indices of bars; trapped water is calculated when a higher bar is found.
🧠 Conceptual
expert
1:30remaining
Time complexity analysis of trapping rain water algorithms
Which statement correctly describes the time complexity of the three common approaches to the trapping rain water problem: brute force, prefix/suffix max arrays, and two-pointer?
ABrute force is O(n^2), prefix/suffix max arrays is O(n^2), two-pointer is O(n)
BBrute force is O(n), prefix/suffix max arrays is O(n^2), two-pointer is O(n^2)
CBrute force is O(n^2), prefix/suffix max arrays is O(n), two-pointer is O(n)
DAll three approaches have O(n) time complexity
Attempts:
2 left
💡 Hint
Consider how many times each element is processed in each approach.