Bird
Raised Fist0
DSA Cprogramming~10 mins

Trapping Rain Water Using Stack in DSA C - Interactive Practice

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

Complete the code to declare the stack pointer for indices.

DSA C
int stack[100];
int top = [1];
Drag options to blanks, or click blank then click option'
A0
B100
C-1
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing top to 0 causes off-by-one errors.
Using 100 as top index causes out-of-bounds.
2fill in blank
medium

Complete the code to push an index onto the stack.

DSA C
stack[++top] = [1];
Drag options to blanks, or click blank then click option'
Ai
Bstack[top]
Ctop
Dheight[i]
Attempts:
3 left
💡 Hint
Common Mistakes
Pushing height[i] instead of index causes logic errors.
Pushing top or stack[top] causes wrong stack content.
3fill in blank
hard

Fix the error in calculating the distance between bars.

DSA C
distance = i - stack[[1]] - 1;
Drag options to blanks, or click blank then click option'
Atop
Btop + 1
Ctop - 1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using top - 1 calculates overly wide distance.
Using top + 1 causes out-of-bounds access.
4fill in blank
hard

Fill both blanks to calculate bounded height and update trapped water.

DSA C
bounded_height = (height[i] < height[stack[top]]) ? height[i] - height[stack[[1]]] : height[stack[top]] - height[stack[[2]]];
water += distance * bounded_height;
Drag options to blanks, or click blank then click option'
Atop - 1
Btop
Ctop + 1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using top subtracts left height (zero water).
Using top - 1 subtracts wrong bar height.
5fill in blank
hard

Fill all three blanks to complete the main loop for trapping rain water.

DSA C
for (int i = 0; i < n; i++) {
    while (top != -1 && height[i] > height[stack[top]]) {
        int top_index = stack[top--];
        if (top == -1) continue;
        int distance = i - stack[[1]] - 1;
        int bounded_height = (height[i] < height[stack[top]]) ? height[i] - height[stack[[2]]] : height[stack[top]] - height[stack[[3]]];
        water += distance * bounded_height;
    }
    stack[++top] = i;
}
Drag options to blanks, or click blank then click option'
Atop
Btop - 1
Ctop + 1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using top - 1 for distance causes wrong width.
Swapping indices in bounded_height causes wrong height calculation.

Practice

(1/5)
1. What is the main purpose of using a stack in the Trapping Rain Water Using Stack algorithm?
easy
A. To reverse the order of the bars
B. To store the total amount of trapped water directly
C. To sort the heights of bars in ascending order
D. To keep track of bars that are not yet processed for water trapping

Solution

  1. Step 1: Understand stack role in the algorithm

    The stack stores indices of bars that are waiting to find a right boundary to trap water.
  2. Step 2: Clarify what the stack does not do

    The stack does not store water amounts or sort bars; it helps find boundaries for trapped water.
  3. Final Answer:

    To keep track of bars that are not yet processed for water trapping -> Option D
  4. Quick Check:

    Stack tracks bars for trapping water [OK]
Hint: Stack holds indices of bars waiting for right boundary [OK]
Common Mistakes:
  • Thinking stack stores water volume directly
  • Assuming stack sorts the bars
  • Believing stack reverses bar order
2. Which of the following C code snippets correctly declares a stack to store indices for trapping rain water?
easy
A. int stack[100]; int top = -1;
B. int stack; int top = 0;
C. int *stack = NULL; int top = 1;
D. int stack[] = {}; int top = -1;

Solution

  1. Step 1: Check stack declaration syntax

    int stack[100]; int top = -1; declares an integer array with fixed size and initializes top to -1, which is correct for stack implementation.
  2. Step 2: Identify incorrect declarations

    Options B, C, and D have wrong types or invalid initializations for stack and top pointer.
  3. Final Answer:

    int stack[100]; int top = -1; -> Option A
  4. Quick Check:

    Array + top = -1 is correct stack setup [OK]
Hint: Use array and top = -1 for empty stack [OK]
Common Mistakes:
  • Using uninitialized or wrong top value
  • Declaring stack as single int instead of array
  • Using empty array syntax invalid in C
3. What is the output of the following code snippet that calculates trapped rain water using stack?
int height[] = {0,1,0,2,1,0,1,3,2,1,2,1};
int n = 12;
// Assume correct trapping rain water stack code runs here
int trapped = trappingRainWater(height, n);
printf("%d\n", trapped);
medium
A. 5
B. 6
C. 4
D. 7

Solution

  1. Step 1: Understand the input heights

    The array represents bar heights where water can be trapped between taller bars.
  2. Step 2: Calculate trapped water manually or recall known result

    The known trapped water for this input is 6 units.
  3. Final Answer:

    6 -> Option B
  4. Quick Check:

    Standard example trapped water = 6 [OK]
Hint: Remember classic example traps 6 units water [OK]
Common Mistakes:
  • Miscounting trapped water units
  • Confusing indexes or heights
  • Assuming output is sum of heights
4. Identify the bug in this snippet of the trapping rain water stack algorithm:
while (top != -1 && height[i] > height[stack[top]]) {
  int topIndex = stack[top--];
  int distance = i - stack[top] - 1;
  int bounded_height = min(height[i], height[stack[top]]) - height[topIndex];
  water += distance * bounded_height;
}
medium
A. Using 'min' function without including its definition or header
B. Incorrect calculation of 'distance' by missing parentheses
C. Wrong decrement of 'top' causing stack underflow
D. Using 'water' variable without initialization

Solution

  1. Step 1: Check function usage

    The code uses 'min' function but C standard library does not have 'min' by default; it needs a macro or function definition.
  2. Step 2: Verify other parts

    Distance calculation and top decrement are correct; water variable usage depends on context but usually initialized.
  3. Final Answer:

    Using 'min' function without including its definition or header -> Option A
  4. Quick Check:

    min() undefined in C without custom code [OK]
Hint: Remember C needs custom min function or macro [OK]
Common Mistakes:
  • Assuming min() is built-in in C
  • Miscalculating distance with wrong parentheses
  • Forgetting to initialize water variable
5. Given the heights array {4, 2, 0, 3, 2, 5}, what is the total trapped rain water calculated using the stack method?
hard
A. 8
B. 7
C. 9
D. 6

Solution

  1. Step 1: Analyze the heights for trapped water

    Water trapped at each position i is max(0, min(left_max[i], right_max[i]) - height[i]), where left_max is tallest bar strictly to the left, right_max strictly to the right. Stack method yields same result.
  2. Step 2: Calculate trapped water step-by-step

    - i=1: min(4,5)-2 = 2
    - i=2: min(4,5)-0 = 4
    - i=3: min(4,5)-3 = 1
    - i=4: min(4,5)-2 = 2
    Total: 2+4+1+2 = 9 units.
  3. Final Answer:

    9 -> Option C
  4. Quick Check:

    Total trapped water = 9 [OK]
Hint: Sum water trapped between boundaries carefully [OK]
Common Mistakes:
  • Double counting trapped water
  • Ignoring zero height bars
  • Misidentifying boundaries
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard