Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to push an operator onto the stack.
DSA C#
stack.[1](ch);Attempts:
3 left
💡 Hint
Common Mistakes
Using pop instead of push removes an element instead of adding.
Using peek only looks at the top element without adding.
✗ Incorrect
The push method adds an element to the top of the stack.
2fill in blank
mediumComplete the code to check if the stack is empty.
DSA C#
if (stack.[1] == 0) { /* stack is empty */ }
Attempts:
3 left
💡 Hint
Common Mistakes
Using Length or Size which are not properties of Stack in C#.
Using Capacity which is for collections like List, not Stack.
✗ Incorrect
The Count property returns the number of elements in the stack.
3fill in blank
hardFix the error in the code to get the top element without removing it.
DSA C#
char top = stack.[1]();Attempts:
3 left
💡 Hint
Common Mistakes
Using pop removes the element, which is not desired here.
Using push or top are not valid Stack methods.
✗ Incorrect
The Peek method returns the top element without removing it.
4fill in blank
hardFill both blanks to correctly check operator precedence.
DSA C#
if (GetPrecedence(ch) [1] GetPrecedence(stack.[2]())) { /* pop stack */ }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' reverses the logic and causes wrong popping.
Using pop() removes the element prematurely.
✗ Incorrect
We compare if current operator has lower precedence than top of stack using '<' and get top with Peek.
5fill in blank
hardFill all three blanks to correctly build the postfix expression.
DSA C#
postfix += [1].ToString(); stack.[2](); stack.[3](ch);
Attempts:
3 left
💡 Hint
Common Mistakes
Using top instead of Peek() is invalid.
Using push instead of pop removes wrong element.
✗ Incorrect
Add top of stack to postfix, pop it, then push current operator.
