Challenge - 5 Problems
Postfix Evaluation 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 Java code that evaluates a postfix expression using a stack?
DSA Java
import java.util.Stack; public class PostfixEval { public static int evaluatePostfix(String expr) { Stack<Integer> stack = new Stack<>(); for (int i = 0; i < expr.length(); i++) { char c = expr.charAt(i); if (Character.isDigit(c)) { stack.push(c - '0'); } else { int val2 = stack.pop(); int val1 = stack.pop(); switch (c) { case '+': stack.push(val1 + val2); break; case '-': stack.push(val1 - val2); break; case '*': stack.push(val1 * val2); break; case '/': stack.push(val1 / val2); break; } } } return stack.pop(); } public static void main(String[] args) { String expr = "231*+9-"; System.out.println(evaluatePostfix(expr)); } }
Attempts:
2 left
💡 Hint
Trace the expression step-by-step using a stack: push digits, pop two for operators, apply operation, push result.
✗ Incorrect
The postfix expression "231*+9-" evaluates as follows: 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
intermediateStack Usage in Postfix Evaluation
Why is a stack the best data structure to evaluate postfix expressions?
Attempts:
2 left
💡 Hint
Think about how postfix expressions require you to use the most recent operands first.
✗ Incorrect
Postfix evaluation requires using the most recently seen operands first when an operator appears. A stack provides last-in, first-out access, making it ideal for this.
❓ Predict Output
advancedOutput of Postfix Evaluation with Division
What is the output of this Java code evaluating the postfix expression "82/3-"?
DSA Java
import java.util.Stack; public class PostfixEvalDiv { public static int evaluatePostfix(String expr) { Stack<Integer> stack = new Stack<>(); for (int i = 0; i < expr.length(); i++) { char c = expr.charAt(i); if (Character.isDigit(c)) { stack.push(c - '0'); } else { int val2 = stack.pop(); int val1 = stack.pop(); switch (c) { case '+': stack.push(val1 + val2); break; case '-': stack.push(val1 - val2); break; case '*': stack.push(val1 * val2); break; case '/': stack.push(val1 / val2); break; } } } return stack.pop(); } public static void main(String[] args) { String expr = "82/3-"; System.out.println(evaluatePostfix(expr)); } }
Attempts:
2 left
💡 Hint
Remember integer division truncates towards zero in Java.
✗ Incorrect
The expression "82/3-" evaluates as: push 8, push 2, divide (8/2=4), push 3, subtract (4-3=1). The final result is 1.
🔧 Debug
advancedIdentify the Error in Postfix Evaluation Code
What error will this Java code produce when evaluating the postfix expression "34+"?
DSA Java
import java.util.Stack; public class PostfixEvalBug { public static int evaluatePostfix(String expr) { Stack<Integer> stack = new Stack<>(); for (int i = 0; i < expr.length(); i++) { char c = expr.charAt(i); if (Character.isDigit(c)) { stack.push(c - '0'); } else { int val1 = stack.pop(); int val2 = stack.pop(); switch (c) { case '+': stack.push(val1 + val2); break; case '-': stack.push(val1 - val2); break; case '*': stack.push(val1 * val2); break; case '/': stack.push(val1 / val2); break; } } } return stack.pop(); } public static void main(String[] args) { String expr = "34+"; System.out.println(evaluatePostfix(expr)); } }
Attempts:
2 left
💡 Hint
Check the order of popping operands for non-commutative operations.
✗ Incorrect
The code pops val1 first (top of stack, second operand) then val2 (first operand), reversing the order. For '+', which is commutative, 4 + 3 = 3 + 4 = 7, so output is correct with no error. The reversed order would cause wrong results for non-commutative operators like '-' or '/'.
🧠 Conceptual
expertHandling Multi-digit Numbers in Postfix Evaluation
Why does the simple stack-based postfix evaluation code fail for expressions with multi-digit numbers like "12 3 +"?
Attempts:
2 left
💡 Hint
Think about how the code reads each character and converts it to a number.
✗ Incorrect
The code reads one character at a time and converts it to a digit, so multi-digit numbers split into separate digits and are treated as separate operands, causing incorrect evaluation.
