Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a stack for storing indices.
DSA C#
Stack<int> stack = new Stack<int>[1];Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets [] instead of parentheses.
Using curly braces {} which are for collections initialization.
Using angle brackets <> which are for generics declaration.
✗ Incorrect
In C#, to create a new stack instance, you use parentheses ().
2fill in blank
mediumComplete the code to pop the top element from the stack.
DSA C#
int top = stack[1]();Attempts:
3 left
💡 Hint
Common Mistakes
Using Peek() which does not remove the element.
Using Dequeue() which is for queues.
Using Remove() which is not a stack method.
✗ Incorrect
Pop() removes and returns the top element from the stack.
3fill in blank
hardFix the error in the condition to check if the stack is not empty.
DSA C#
while (stack[1] > 0) { /* logic */ }
Attempts:
3 left
💡 Hint
Common Mistakes
Using Count() with parentheses causes a compile error.
Using Length or Size which do not exist for Stack.
✗ Incorrect
Stack in C# has a Count property (without parentheses) to get the number of elements.
4fill in blank
hardFill both blanks to calculate trapped water correctly.
DSA C#
int distance = currentIndex - stack[1] - 1; int boundedHeight = Math.Min(height[currentIndex], height[stack[2]]) - height[top];
Attempts:
3 left
💡 Hint
Common Mistakes
Using Pop() instead of Peek() for distance calculation.
Using Pop() twice which removes two elements incorrectly.
✗ Incorrect
Peek() accesses the left bound index (after popping the bottom into 'top') for distance calculation; Peek() reads it for height comparison.
5fill in blank
hardFill all three blanks to update the total trapped water correctly.
DSA C#
waterTrapped += distance * boundedHeight; stack[1](currentIndex); currentIndex[2]; int top = stack[3]();
Attempts:
3 left
💡 Hint
Common Mistakes
Using Pop instead of Push to add index.
Using -- instead of ++ to move forward.
Using Pop instead of Peek to read top.
✗ Incorrect
Push adds current index; ++ increments currentIndex; Peek reads top without removing.
