Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare the stack used for indices.
DSA C++
std::stack<int> [1];Attempts:
3 left
💡 Hint
Common Mistakes
Using stack of heights instead of indices
Naming stack as 'water' which is confusing
✗ Incorrect
The stack stores indices of bars to calculate trapped water.
2fill in blank
mediumComplete the code to get the current height from the input vector.
DSA C++
int currentHeight = height[[1]];Attempts:
3 left
💡 Hint
Common Mistakes
Using 'top' which is a stack variable, not loop index
Using 'j' which is undefined here
✗ Incorrect
We use 'i' as the loop variable to access current height.
3fill in blank
hardFix the error in the while loop condition to check stack and height.
DSA C++
while (!indices.empty() && height[[1]] > height[indices.top()]) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'top' as index which is a method call
Using 'indices.top()' inside height[] which is correct but not the blank here
✗ Incorrect
We compare current height at i with height at top index of stack.
4fill in blank
hardFill both blanks to calculate the distance and bounded height for trapped water.
DSA C++
int distance = i - [1] - 1; int bounded_height = std::min(height[i], height[[2]]) - height[top];
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'top' instead of 'indices.top()' for distance
Using 'top' instead of 'indices.top()' for bounded height calculation
✗ Incorrect
Distance is between current index i and stack top index; bounded height uses min of current and new top heights.
5fill in blank
hardFill all three blanks to update total trapped water and push current index.
DSA C++
water += distance * bounded_height; indices.[1](); indices.[2](i); return [3];
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'pop' instead of 'push' or vice versa
Returning wrong variable name like 'totalWater' instead of 'water'
✗ Incorrect
We pop the top index, push current index i, and return total water stored in 'water'.
