Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to push an operator onto the stack.
DSA Typescript
stack.[1](operator);Attempts:
3 left
💡 Hint
Common Mistakes
Using pop instead of push
Using peek which only views the top element
✗ Incorrect
We use push to add an element to the top of the stack.
2fill in blank
mediumComplete the code to check if the stack is empty.
DSA Typescript
if (stack.length [1] 0) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using > or < which check for non-empty or negative length
Using !== which checks for not equal
✗ Incorrect
We check if the stack length is exactly zero to know if it is empty.
3fill in blank
hardFix the error in the code to pop the top element from the stack.
DSA Typescript
const top = stack.[1]();Attempts:
3 left
💡 Hint
Common Mistakes
Using push which adds instead of removes
Using shift/unshift which operate on the start of array
✗ Incorrect
To remove and get the top element from a stack, use pop().
4fill in blank
hardFill both blanks to compare operator precedence correctly.
DSA Typescript
if (precedence[stack[stack.length - 1]] [1] precedence[currentOp]) { stack.[2](); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' which reverses the logic
Using push instead of pop
✗ Incorrect
We pop from the stack if the top operator has higher precedence than the current operator.
5fill in blank
hardFill all three blanks to build the postfix expression correctly.
DSA Typescript
while (stack.length > 0) { postfix += stack.[1](); postfix += [2]; stack.[3](); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using semicolon instead of space
Using push instead of pop
✗ Incorrect
We pop each operator, add a space after it, and continue until stack is empty.
