Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to push operands onto the stack.
DSA C++
if (isdigit(exp[i])) { int operand = exp[i] - '0'; s.[1](operand); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() instead of push()
Trying to access top() to add elements
✗ Incorrect
We use push() to add operands to the stack.
2fill in blank
mediumComplete the code to get the value of the top element from the stack.
DSA C++
int val2 = s.[1]();Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() which returns void
Using empty() which checks if stack is empty
✗ Incorrect
top() returns the top element without removing it; pop() removes it but returns void.
3fill in blank
hardFix the error in the code to correctly pop the element after reading it.
DSA C++
int val1 = s.top();
s.[1]();Attempts:
3 left
💡 Hint
Common Mistakes
Calling top() again instead of pop()
Using push() mistakenly
✗ Incorrect
After reading the top element, pop() removes it from the stack.
4fill in blank
hardFill all three blanks to perform addition on two popped values and push the result.
DSA C++
int val2 = s.top(); s.[1](); int val1 = s.top(); s.[2](); int res = val1 [3] val2; s.push(res);
Attempts:
3 left
💡 Hint
Common Mistakes
Using push() instead of pop() to remove elements
Using '-' instead of '+' for addition
✗ Incorrect
We pop both values, add them with '+', then push the result.
5fill in blank
hardFill all three blanks to evaluate the postfix expression correctly for multiplication.
DSA C++
int val2 = s.top(); s.[1](); int val1 = s.top(); s.[2](); int res = val1 [3] val2; s.push(res);
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '*'
Using push() instead of pop() to remove elements
✗ Incorrect
Pop both operands, multiply with '*', then push the result.
