Challenge - 5 Problems
Postfix Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Postfix Conversion for Simple Expression
What is the output of the postfix conversion for the infix expression
a+b*c using a stack?DSA Go
func infixToPostfix(expression string) string {
precedence := map[rune]int{'+' : 1, '-' : 1, '*' : 2, '/' : 2}
stack := []rune{}
result := ""
for _, ch := range expression {
if (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') {
result += string(ch)
} else {
for len(stack) > 0 && precedence[stack[len(stack)-1]] >= precedence[ch] {
result += string(stack[len(stack)-1])
stack = stack[:len(stack)-1]
}
stack = append(stack, ch)
}
}
for len(stack) > 0 {
result += string(stack[len(stack)-1])
stack = stack[:len(stack)-1]
}
return result
}
fmt.Println(infixToPostfix("a+b*c"))Attempts:
2 left
💡 Hint
Remember that multiplication (*) has higher precedence than addition (+).
✗ Incorrect
In infix expression 'a+b*c', multiplication has higher precedence, so 'b*c' is evaluated first, then added to 'a'. The postfix is 'abc*+'.
❓ Predict Output
intermediatePostfix Conversion Output with Parentheses
What is the output of the postfix conversion for the infix expression
(a+b)*c using a stack?DSA Go
func infixToPostfix(expression string) string {
precedence := map[rune]int{'+' : 1, '-' : 1, '*' : 2, '/' : 2}
stack := []rune{}
result := ""
for _, ch := range expression {
if (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') {
result += string(ch)
} else if ch == '(' {
stack = append(stack, ch)
} else if ch == ')' {
for len(stack) > 0 && stack[len(stack)-1] != '(' {
result += string(stack[len(stack)-1])
stack = stack[:len(stack)-1]
}
stack = stack[:len(stack)-1] // pop '('
} else {
for len(stack) > 0 && stack[len(stack)-1] != '(' && precedence[stack[len(stack)-1]] >= precedence[ch] {
result += string(stack[len(stack)-1])
stack = stack[:len(stack)-1]
}
stack = append(stack, ch)
}
}
for len(stack) > 0 {
result += string(stack[len(stack)-1])
stack = stack[:len(stack)-1]
}
return result
}
fmt.Println(infixToPostfix("(a+b)*c"))Attempts:
2 left
💡 Hint
Parentheses change the order of operations; evaluate inside parentheses first.
✗ Incorrect
The expression '(a+b)*c' means add 'a' and 'b' first, then multiply by 'c'. The postfix is 'ab+c*'.
🧠 Conceptual
advancedUnderstanding Operator Precedence in Postfix Conversion
Which operator precedence rule is correctly applied in the infix to postfix conversion using a stack?
Attempts:
2 left
💡 Hint
Think about when to pop operators from the stack based on their precedence compared to the incoming operator.
✗ Incorrect
When a new operator arrives, operators on the stack with higher or equal precedence are popped first to maintain correct order.
🔧 Debug
advancedIdentify the Error in Postfix Conversion Code
Given the following code snippet for infix to postfix conversion, what error will it cause when processing the expression
a*b/c?
func infixToPostfix(expression string) string {
precedence := map[rune]int{'+' : 1, '-' : 1, '*' : 2, '/' : 2}
stack := []rune{}
result := ""
for _, ch := range expression {
if (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') {
result += string(ch)
} else {
for len(stack) > 0 && precedence[stack[len(stack)-1]] > precedence[ch] {
result += string(stack[len(stack)-1])
stack = stack[:len(stack)-1]
}
stack = append(stack, ch)
}
}
for len(stack) > 0 {
result += string(stack[len(stack)-1])
stack = stack[:len(stack)-1]
}
return result
}
fmt.Println(infixToPostfix("a*b/c"))Attempts:
2 left
💡 Hint
Check the comparison operator in the while loop condition for popping from stack.
✗ Incorrect
The code uses '>' instead of '>=' in precedence comparison, so it fails to pop operators with equal precedence (like '*' and '/'), causing output 'abc/*' instead of correct 'ab*c/'.
🚀 Application
expertPostfix Expression Length After Conversion
Given the infix expression
(a+b)*(c-d)/e, how many characters will the resulting postfix expression have after conversion using a stack?Attempts:
2 left
💡 Hint
Count operands and operators carefully after conversion.
✗ Incorrect
The postfix expression is 'ab+cd-*e/', which has 9 characters (5 operands: a,b,c,d,e and 4 operators: +,-,*,/).
