Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to push an opening parenthesis onto the stack.
DSA C#
if (ch == '(') { stack.[1](ch); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using Pop instead of Push will remove an element instead of adding.
Using Peek only looks at the top element without adding.
✗ Incorrect
We use stack.Push() to add an element to the top of the stack.
2fill in blank
mediumComplete the code to check if the stack is empty before popping.
DSA C#
if (stack.[1] == 0) { return false; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using Length or Size causes compile errors because Stack does not have these properties.
✗ Incorrect
Stack.Count gives the number of elements currently in the stack.
3fill in blank
hardFix the error in popping the top element from the stack.
DSA C#
char top = stack.[1]();Attempts:
3 left
💡 Hint
Common Mistakes
Using Peek does not remove the element, so the stack remains unchanged.
Remove and Delete are not valid Stack methods in C#.
✗ Incorrect
stack.Pop() removes and returns the top element from the stack.
4fill in blank
hardFill both blanks to check if the current closing parenthesis matches the last opening one.
DSA C#
if (ch == ')' && top != [1]) { return [2]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Returning true here would incorrectly say parentheses are balanced.
Checking against '[' is wrong for ')'.
✗ Incorrect
A closing ')' must match an opening '('. If not, return false.
5fill in blank
hardFill all three blanks to complete the dictionary comprehension that maps opening to closing parentheses.
DSA C#
var pairs = new Dictionary<char, char> {
{ [1], ')' },
{ [2], '}' },
{ [3], ']' }
};Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' is incorrect because it is not a standard bracket in this problem.
Mixing keys and values will cause errors.
✗ Incorrect
The dictionary maps '(' to ')', '{' to '}', and '[' to ']'.
