Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to push an operator onto the stack.
DSA Java
stack.[1](ch);Attempts:
3 left
💡 Hint
Common Mistakes
Using pop instead of push removes an element instead of adding.
Using peek does not add anything to the stack.
✗ 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 Java
if(stack.[1]()) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using peek() to check emptiness causes errors if stack is empty.
Using pop() removes elements, not checks emptiness.
✗ Incorrect
The empty() method returns true if the stack has no elements.
3fill in blank
hardFix the error in the code to get the precedence of an operator.
DSA Java
int precedence = [1](op);Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name instead of a method call causes errors.
Using incorrect method names leads to compilation errors.
✗ Incorrect
The method to get operator precedence is named getPrecedence in this code.
4fill in blank
hardFill both blanks to correctly pop from stack and append to result.
DSA Java
result += stack.[1](); stack.[2]();
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() twice removes two elements instead of one.
Using push() instead of pop() adds elements incorrectly.
✗ Incorrect
pop() removes the top element from the stack and returns it. Using peek() followed by pop() is redundant here.
5fill in blank
hardFill all three blanks to correctly check operator precedence and pop from stack.
DSA Java
while(!stack.[1]() && getPrecedence(ch) <= getPrecedence(stack.[2]())) { result += stack.[3](); }
Attempts:
3 left
💡 Hint
Common Mistakes
Not checking empty() causes errors when stack is empty.
Using push() instead of pop() causes infinite loops.
✗ Incorrect
empty() checks if stack is empty, peek() gets top element, pop() removes it.
