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 Go code that evaluates a postfix expression using a stack?
DSA Go
package main import ( "fmt" "strconv" ) func evaluatePostfix(expr string) int { stack := []int{} for _, ch := range expr { if ch >= '0' && ch <= '9' { num, _ := strconv.Atoi(string(ch)) stack = append(stack, num) } else { b := stack[len(stack)-1] stack = stack[:len(stack)-1] a := stack[len(stack)-1] stack = stack[:len(stack)-1] switch ch { case '+': stack = append(stack, a+b) case '-': stack = append(stack, a-b) case '*': stack = append(stack, a*b) case '/': stack = append(stack, a/b) } } } return stack[0] } func main() { expr := "231*+9-" result := evaluatePostfix(expr) fmt.Println(result) }
Attempts:
2 left
💡 Hint
Trace the stack operations step-by-step for each character in the expression.
✗ Incorrect
The postfix expression "231*+9-" is evaluated as follows:
- Push 2
- Push 3
- Push 1
- Pop 1 and 3, multiply: 3*1=3, push 3
- Pop 3 and 2, add: 2+3=5, push 5
- Push 9
- Pop 9 and 5, subtract: 5-9=-4, push -4
Final result is -4.
🧠 Conceptual
intermediateStack Behavior in Postfix Evaluation
During the evaluation of a postfix expression using a stack, what happens when an operator is encountered?
Attempts:
2 left
💡 Hint
Think about how operators combine numbers in postfix notation.
✗ Incorrect
In postfix evaluation, when an operator is found, the last two numbers are taken from the stack, the operation is performed, and the result is pushed back onto the stack.
🔧 Debug
advancedIdentify the Error in Postfix Evaluation Code
What error will the following Go code produce when evaluating the postfix expression "82/"?
func evaluatePostfix(expr string) int {
stack := []int{}
for _, ch := range expr {
if ch >= '0' && ch <= '9' {
num, _ := strconv.Atoi(string(ch))
stack = append(stack, num)
} else {
a := stack[len(stack)-1]
stack = stack[:len(stack)-1]
b := stack[len(stack)-1]
stack = stack[:len(stack)-1]
switch ch {
case '+':
stack = append(stack, a+b)
case '-':
stack = append(stack, a-b)
case '*':
stack = append(stack, a*b)
case '/':
stack = append(stack, a/b)
}
}
}
return stack[0]
}
Attempts:
2 left
💡 Hint
Check the order of popping operands from the stack.
✗ Incorrect
The code pops operands in wrong order: it pops 'a' first then 'b', but for division, the second popped should be the divisor. This causes incorrect stack access and panic.
❓ Predict Output
advancedResult of Complex Postfix Expression
What is the output of the following Go code evaluating the postfix expression "562+*84/-"?
DSA Go
package main import ( "fmt" "strconv" ) func evaluatePostfix(expr string) int { stack := []int{} for _, ch := range expr { if ch >= '0' && ch <= '9' { num, _ := strconv.Atoi(string(ch)) stack = append(stack, num) } else { b := stack[len(stack)-1] stack = stack[:len(stack)-1] a := stack[len(stack)-1] stack = stack[:len(stack)-1] switch ch { case '+': stack = append(stack, a+b) case '-': stack = append(stack, a-b) case '*': stack = append(stack, a*b) case '/': stack = append(stack, a/b) } } } return stack[0] } func main() { expr := "562+*84/-" result := evaluatePostfix(expr) fmt.Println(result) }
Attempts:
2 left
💡 Hint
Evaluate step-by-step, carefully applying each operator.
✗ Incorrect
Stepwise evaluation:
- Push 5,6,2
- Pop 2 and 6, add: 6+2=8, push 8
- Pop 8 and 5, multiply: 5*8=40, push 40
- Push 8,4
- Pop 4 and 8, subtract: 8-4=4, push 4
- Pop 4 and 40, divide: 40/4=10, push 10
Final result is 10.
🧠 Conceptual
expertWhy Use Stack for Postfix Evaluation?
Why is a stack the ideal data structure for evaluating postfix expressions?
Attempts:
2 left
💡 Hint
Think about how postfix expressions require the most recent operands first.
✗ Incorrect
Stacks follow Last-In-First-Out order, which matches the need to use the most recent operands first when an operator is encountered in postfix evaluation.
