Challenge - 5 Problems
Postfix 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 C# code that evaluates a postfix expression using a stack?
DSA C#
using System; using System.Collections.Generic; class Program { static int EvaluatePostfix(string expr) { Stack<int> stack = new Stack<int>(); foreach (char c in expr) { if (char.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(); } static void Main() { string postfix = "231*+9-"; Console.WriteLine(EvaluatePostfix(postfix)); } }
Attempts:
2 left
💡 Hint
Trace the expression step by step: 2 3 1 * + 9 -
✗ Incorrect
The postfix expression evaluates as follows: 3*1=3, then 2+3=5, then 5-9=-4.
❓ Predict Output
intermediateResult of Postfix Expression with Division
What is the output of this C# code evaluating a postfix expression with division?
DSA C#
using System; using System.Collections.Generic; class Program { static int EvaluatePostfix(string expr) { Stack<int> stack = new Stack<int>(); foreach (char c in expr) { if (char.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(); } static void Main() { string postfix = "82/3-"; Console.WriteLine(EvaluatePostfix(postfix)); } }
Attempts:
2 left
💡 Hint
Remember integer division truncates towards zero.
✗ Incorrect
The expression evaluates as 8/2=4, then 4-3=1. But integer division 8/2=4, so final result is 1.
🔧 Debug
advancedIdentify the Error in Postfix Evaluation Code
What error will this C# code produce when evaluating the postfix expression "1+"?
DSA C#
using System; using System.Collections.Generic; class Program { static int EvaluatePostfix(string expr) { Stack<int> stack = new Stack<int>(); foreach (char c in expr) { if (char.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(); } static void Main() { string postfix = "1+"; Console.WriteLine(EvaluatePostfix(postfix)); } }
Attempts:
2 left
💡 Hint
Check the order of popping operands from the stack.
✗ Incorrect
The code pops operands in wrong order: val1 = stack.Pop() then val2 = stack.Pop(). This reverses operand order causing wrong calculation or empty stack pop.
🧠 Conceptual
advancedStack Behavior in Postfix Evaluation
Which statement best describes the role of the stack in evaluating a postfix expression?
Attempts:
2 left
💡 Hint
Think about what you push and pop during evaluation.
✗ Incorrect
In postfix evaluation, operands are pushed onto the stack. When an operator is encountered, operands are popped, operation done, and result pushed back. So stack holds operands and intermediate results.
❓ Predict Output
expertOutput of Complex Postfix Expression Evaluation
What is the output of this C# code evaluating the postfix expression "345+*62/-"?
DSA C#
using System; using System.Collections.Generic; class Program { static int EvaluatePostfix(string expr) { Stack<int> stack = new Stack<int>(); foreach (char c in expr) { if (char.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(); } static void Main() { string postfix = "345+*62/-"; Console.WriteLine(EvaluatePostfix(postfix)); } }
Attempts:
2 left
💡 Hint
Evaluate stepwise: 3 (4+5) * then 6 2 / -
✗ Incorrect
Stepwise evaluation: 4+5=9, then 3*9=27, then 6/2=3, then 27-3=24.
