Challenge - 5 Problems
Postfix Stack 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++
#include <iostream> #include <stack> #include <string> using namespace std; int evaluatePostfix(string expr) { stack<int> s; for (char c : expr) { if (isdigit(c)) { s.push(c - '0'); } else { int val2 = s.top(); s.pop(); int val1 = s.top(); s.pop(); switch (c) { case '+': s.push(val1 + val2); break; case '-': s.push(val1 - val2); break; case '*': s.push(val1 * val2); break; case '/': s.push(val1 / val2); break; } } } return s.top(); } int main() { string expr = "231*+9-"; cout << evaluatePostfix(expr) << endl; return 0; }
Attempts:
2 left
💡 Hint
Trace the stack operations step-by-step for each character in the expression.
✗ Incorrect
The postfix expression "231*+9-" evaluates as follows: push 2, push 3, push 1, multiply top two (3*1=3), push result, add top two (2+3=5), push 9, subtract top two (5-9=-4). Final result is -4.
❓ Predict Output
intermediateResult of Postfix Expression with Division
What is the output of this C++ code evaluating the postfix expression "82/3-"?
DSA C++
#include <iostream> #include <stack> #include <string> using namespace std; int evaluatePostfix(string expr) { stack<int> s; for (char c : expr) { if (isdigit(c)) { s.push(c - '0'); } else { int val2 = s.top(); s.pop(); int val1 = s.top(); s.pop(); switch (c) { case '+': s.push(val1 + val2); break; case '-': s.push(val1 - val2); break; case '*': s.push(val1 * val2); break; case '/': s.push(val1 / val2); break; } } } return s.top(); } int main() { string expr = "82/3-"; cout << evaluatePostfix(expr) << endl; return 0; }
Attempts:
2 left
💡 Hint
Remember integer division truncates towards zero in C++.
✗ Incorrect
The expression "82/3-" evaluates as: push 8, push 2, divide (8/2=4), push 3, subtract (4-3=1). Final result is 1.
🔧 Debug
advancedIdentify the Runtime Error in Postfix Evaluation
What runtime error will occur when running this C++ code with input "3+"?
DSA C++
#include <iostream> #include <stack> #include <string> using namespace std; int evaluatePostfix(string expr) { stack<int> s; for (char c : expr) { if (isdigit(c)) { s.push(c - '0'); } else { int val2 = s.top(); s.pop(); int val1 = s.top(); s.pop(); switch (c) { case '+': s.push(val1 + val2); break; case '-': s.push(val1 - val2); break; case '*': s.push(val1 * val2); break; case '/': s.push(val1 / val2); break; } } } return s.top(); } int main() { string expr = "3+"; cout << evaluatePostfix(expr) << endl; return 0; }
Attempts:
2 left
💡 Hint
Check how many operands are available before each operator is applied.
✗ Incorrect
Processing '3+': push 3 (stack: [3]). For '+': pop val2 = 3 (stack empty), then attempt to pop val1 from empty stack causes runtime error: stack underflow.
❓ Predict Output
advancedOutput of Complex Postfix Expression
What is the output of this C++ code evaluating the postfix expression "562+*84/-"?
DSA C++
#include <iostream> #include <stack> #include <string> using namespace std; int evaluatePostfix(string expr) { stack<int> s; for (char c : expr) { if (isdigit(c)) { s.push(c - '0'); } else { int val2 = s.top(); s.pop(); int val1 = s.top(); s.pop(); switch (c) { case '+': s.push(val1 + val2); break; case '-': s.push(val1 - val2); break; case '*': s.push(val1 * val2); break; case '/': s.push(val1 / val2); break; } } } return s.top(); } int main() { string expr = "562+*84/-"; cout << evaluatePostfix(expr) << endl; return 0; }
Attempts:
2 left
💡 Hint
Evaluate step-by-step: push digits, apply operators in order.
✗ Incorrect
Stepwise evaluation:
- push 5 (stack: [5])
- push 6 ([5,6])
- push 2 ([5,6,2])
- '+' : 6+2=8 ([5,8])
- '*' : 5*8=40 ([40])
- push 8 ([40,8])
- push 4 ([40,8,4])
- '/' : 8/4=2 ([40,2])
- '-' : 40-2=38 ([38])
Final result is 38.
🧠 Conceptual
expertStack Size During Postfix Evaluation
Given the postfix expression "345+*62/-", what is the maximum size of the stack during its evaluation?
Attempts:
2 left
💡 Hint
Count how many operands are pushed before each operator reduces the stack size.
✗ Incorrect
Stepwise stack sizes:
- push 3 (1)
- push 4 (2)
- push 5 (3)
- '+' (pop 2, push 1: 2)
- '*' (pop 2, push 1: 1)
- push 6 (2)
- push 2 (3)
- '/' (pop 2, push 1: 2)
- '-' (pop 2, push 1: 1)
Maximum size is 3.
