Bird
0
0
DSA Cprogramming~10 mins

Trapping Rain Water Problem in DSA C - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare the array size variable for the heights array.

DSA C
int height[] = {0,1,0,2,1,0,1,3,2,1,2,1};
int n = [1];
Drag options to blanks, or click blank then click option'
Asizeof(height) / sizeof(height[0])
Bsizeof(height)
Clength(height)
Dcount(height)
Attempts:
3 left
💡 Hint
Common Mistakes
Using sizeof(height) alone gives total bytes, not element count.
Using length() or count() which are not valid in C.
2fill in blank
medium

Complete the code to initialize the left max array first element.

DSA C
int leftMax[n];
leftMax[0] = [1];
Drag options to blanks, or click blank then click option'
A0
Bn
Cheight[1]
Dheight[0]
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing leftMax[0] to 0 instead of height[0].
Using height[1] which is the wrong index.
3fill in blank
hard

Fix the error in the loop that fills the right max array from the right side.

DSA C
int rightMax[n];
rightMax[n-1] = height[n-1];
for (int i = n-2; i >= 0; i--) {
    rightMax[i] = [1];
}
Drag options to blanks, or click blank then click option'
Aheight[i] < rightMax[i+1] ? height[i] : rightMax[i+1]
BrightMax[i+1]
Cheight[i] > rightMax[i+1] ? height[i] : rightMax[i+1]
Dheight[i]
Attempts:
3 left
💡 Hint
Common Mistakes
Using less than comparison instead of greater than.
Assigning only height[i] or rightMax[i+1] without comparison.
4fill in blank
hard

Fill both blanks to calculate trapped water at each index and add to total.

DSA C
int water = 0;
for (int i = 0; i < n; i++) {
    water += ([1] < [2] ? [1] : [2]) - height[i];
}
Drag options to blanks, or click blank then click option'
AleftMax[i]
BrightMax[i]
Cheight[i]
Dwater
Attempts:
3 left
💡 Hint
Common Mistakes
Using height[i] instead of leftMax or rightMax in min calculation.
Subtracting wrong values or adding height instead of subtracting.
5fill in blank
hard

Fill all three blanks to complete the trapping rain water function.

DSA C
int trap(int* height, int n) {
    int leftMax[n], rightMax[n];
    leftMax[0] = [1];
    for (int i = 1; i < n; i++) {
        leftMax[i] = (height[i] > leftMax[i-1]) ? height[i] : leftMax[i-1];
    }
    rightMax[n-1] = [2];
    for (int i = n-2; i >= 0; i--) {
        rightMax[i] = (height[i] > rightMax[i+1]) ? height[i] : rightMax[i+1];
    }
    int water = 0;
    for (int i = 0; i < n; i++) {
        water += ([3] < rightMax[i] ? [3] : rightMax[i]) - height[i];
    }
    return water;
}
Drag options to blanks, or click blank then click option'
Aheight[0]
Bheight[n-1]
CleftMax[i]
DrightMax[i]
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing leftMax or rightMax with wrong indices.
Using wrong variables in water calculation.