Challenge - 5 Problems
Postfix Stack Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this postfix evaluation code?
Consider the postfix expression
"231*+9-". What will be the final result after evaluating it using a stack?DSA Javascript
function evaluatePostfix(expression) {
const stack = [];
for (const char of expression) {
if (!isNaN(char)) {
stack.push(Number(char));
} else {
const b = stack.pop();
const a = stack.pop();
switch (char) {
case '+': stack.push(a + b); break;
case '-': stack.push(a - b); break;
case '*': stack.push(a * b); break;
case '/': stack.push(Math.floor(a / b)); break;
}
}
}
return stack.pop();
}
console.log(evaluatePostfix("231*+9-"));Attempts:
2 left
💡 Hint
Remember to process the expression left to right, pushing numbers and applying operators to the top two stack values.
✗ Incorrect
The postfix expression is evaluated step-by-step: push 2, push 3, push 1, multiply top two (3*1=3), stack now [2,3], add top two (2+3=5), stack now [5], push 9, subtract top two (5-9=-4), final result is -4.
🧠 Conceptual
intermediateHow many items remain in the stack after processing the postfix expression "82/3-"?
Given the postfix expression
"82/3-", how many items are left in the stack after fully evaluating it?Attempts:
2 left
💡 Hint
The stack should have exactly one item after complete evaluation of a valid postfix expression.
✗ Incorrect
After evaluating the expression, all operations reduce the stack until only one final result remains.
❓ Predict Output
advancedWhat is the output of this postfix evaluation code?
What will this code produce when evaluating the postfix expression "34+5*6"?
DSA Javascript
function evaluatePostfix(expression) {
const stack = [];
for (const char of expression) {
if (!isNaN(char)) {
stack.push(Number(char));
} else {
const b = stack.pop();
const a = stack.pop();
switch (char) {
case '+': stack.push(a + b); break;
case '-': stack.push(a - b); break;
case '*': stack.push(a * b); break;
case '/': stack.push(Math.floor(a / b)); break;
}
}
}
return stack.pop();
}
console.log(evaluatePostfix("34+5*6"));Attempts:
2 left
💡 Hint
Simulate the stack step-by-step. The code does not validate if the expression is correct; it just processes each character.
✗ Incorrect
The expression "34+5*6" is not a fully valid postfix expression (should reduce to one value), but the code runs without error: push 3, push 4, + → [7]; push 5, * → [35]; push 6 → [35,6]; pop returns 6.
🚀 Application
advancedWhich postfix expression evaluates to 15 using the stack evaluation method?
Select the postfix expression that evaluates to 15 when processed by a stack-based postfix evaluator.
Attempts:
2 left
💡 Hint
Evaluate each expression step-by-step or mentally simulate the stack operations.
✗ Incorrect
A: "93+" → 9+3=12
B: "75+3*" → (7+5)*3=36
C: "54+6+" → (5+4)+6=15
D: "82/3+" → (8/2)+3=7 (using floor division)
Only C evaluates to 15.
🧠 Conceptual
expertWhat is the time complexity of evaluating a postfix expression of length n using a stack?
Given a postfix expression with n characters, what is the time complexity of evaluating it using a stack-based algorithm?
Attempts:
2 left
💡 Hint
Each character is processed once with constant time stack operations.
✗ Incorrect
Each character in the postfix expression is processed exactly once, and stack operations (push/pop) take constant time, so total time is linear in n.
