Challenge - 5 Problems
Postfix Expression Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Postfix Expression Evaluation
What is the output of the following TypeScript code that evaluates a postfix expression using a stack?
DSA Typescript
function evaluatePostfix(expression: string): number {
const stack: number[] = [];
for (const char of expression) {
if (!isNaN(Number(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
Trace the stack operations step-by-step for each character in the expression.
✗ Incorrect
The postfix expression is evaluated by pushing numbers onto the stack and applying operators to the top two numbers. For "231*+9-":
- Push 2
- Push 3
- Push 1
- Operator '*': pop 1 and 3, multiply 3*1=3, push 3
- Operator '+': pop 3 and 2, add 2+3=5, push 5
- Push 9
- Operator '-': pop 9 and 5, subtract 5-9=-4, push -4
Result is -4 (option D).
❓ Predict Output
intermediateResult of Postfix Expression with Division
What is the output of this TypeScript code evaluating the postfix expression "82/3-"?
DSA Typescript
function evaluatePostfix(expression: string): number {
const stack: number[] = [];
for (const char of expression) {
if (!isNaN(Number(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("82/3-"));Attempts:
2 left
💡 Hint
Remember that division uses Math.floor for integer division.
✗ Incorrect
Step-by-step evaluation:
- Push 8
- Push 2
- Operator '/': pop 2 and 8, compute Math.floor(8/2) = 4, push 4
- Push 3
- Operator '-': pop 3 and 4, compute 4 - 3 = 1, push 1
Final result is 1 (option B).
❓ Predict Output
advancedOutput of Postfix Expression with Multiple Operators
What is the output of the following TypeScript code evaluating the postfix expression "562+*84/-"?
DSA Typescript
function evaluatePostfix(expression: string): number {
const stack: number[] = [];
for (const char of expression) {
if (!isNaN(Number(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("562+*84/-"));Attempts:
2 left
💡 Hint
Evaluate each operator carefully and track the stack after each step.
✗ Incorrect
Step-by-step:
- Push 5
- Push 6
- Push 2
- Operator '+': pop 2 and 6, 6+2=8, push 8
- Operator '*': pop 8 and 5, 5*8=40, push 40
- Push 8
- Push 4
- Operator '/': pop 4 and 8, Math.floor(8/4)=2, push 2
- Operator '-': pop 2 and 40, 40-2=38, push 38
Final result is 38 (option A).
❓ Predict Output
advancedOutput of Postfix Expression with Negative Result
What is the output of this TypeScript code evaluating the postfix expression "93-5*"?
DSA Typescript
function evaluatePostfix(expression: string): number {
const stack: number[] = [];
for (const char of expression) {
if (!isNaN(Number(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("93-5*"));Attempts:
2 left
💡 Hint
Calculate subtraction before multiplication.
✗ Incorrect
Step-by-step:
- Push 9
- Push 3
- Operator '-': 9 - 3 = 6, push 6
- Push 5
- Operator '*': 6 * 5 = 30, push 30
Output is 30 (option C).
🧠 Conceptual
expertStack Size During Postfix Evaluation
During the evaluation of the postfix expression "345*+62/-", what is the maximum size of the stack at any point?
Attempts:
2 left
💡 Hint
Track the stack size after each push and pop operation.
✗ Incorrect
Step-by-step stack size:
- Push 3 (size 1)
- Push 4 (size 2)
- Push 5 (size 3)
- Operator '*': pop 5 and 4, push 20 (size 2)
- Operator '+': pop 20 and 3, push 23 (size 1)
- Push 6 (size 2)
- Push 2 (size 3)
- Operator '/': pop 2 and 6, push 3 (size 2)
- Operator '-': pop 3 and 23, push 20 (size 1)
Maximum stack size reached is 3 (option A).
