Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
In C, to get the number of elements in an array, divide the total size by the size of one element.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing leftMax[0] to 0 instead of height[0].
Using height[1] which is the wrong index.
✗ Incorrect
The first element of leftMax is the first height value because no bars are to the left.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using less than comparison instead of greater than.
Assigning only height[i] or rightMax[i+1] without comparison.
✗ Incorrect
We assign the max of current height and the next right max to rightMax[i].
4fill in blank
hardFill 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'
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.
✗ Incorrect
Water trapped is min of leftMax and rightMax minus current height.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing leftMax or rightMax with wrong indices.
Using wrong variables in water calculation.
✗ Incorrect
Initialize leftMax[0] and rightMax[n-1] with boundary heights. Use leftMax[i] in water calculation.
