Challenge - 5 Problems
Infix to Postfix Master
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 this Rust code that converts the infix expression
a+b*c to postfix notation?DSA Rust
fn precedence(op: char) -> i32 {
match op {
'+' | '-' => 1,
'*' | '/' => 2,
_ => 0,
}
}
fn infix_to_postfix(expr: &str) -> String {
let mut stack: Vec<char> = Vec::new();
let mut result = String::new();
for ch in expr.chars() {
if ch.is_alphanumeric() {
result.push(ch);
} else {
while !stack.is_empty() && precedence(*stack.last().unwrap()) >= precedence(ch) {
result.push(stack.pop().unwrap());
}
stack.push(ch);
}
}
while !stack.is_empty() {
result.push(stack.pop().unwrap());
}
result
}
fn main() {
let expr = "a+b*c";
let postfix = infix_to_postfix(expr);
println!("{}", postfix);
}Attempts:
2 left
💡 Hint
Remember that multiplication (*) has higher precedence than addition (+).
✗ Incorrect
The multiplication operator '*' has higher precedence than '+', so 'b*c' is evaluated first, then added to 'a'. The postfix expression is 'abc*+'.
❓ Predict Output
intermediatePostfix Conversion Output with Parentheses
What is the output of this Rust code converting the infix expression
(a+b)*c to postfix notation?DSA Rust
fn precedence(op: char) -> i32 {
match op {
'+' | '-' => 1,
'*' | '/' => 2,
_ => 0,
}
}
fn infix_to_postfix(expr: &str) -> String {
let mut stack: Vec<char> = Vec::new();
let mut result = String::new();
for ch in expr.chars() {
if ch.is_alphanumeric() {
result.push(ch);
} else if ch == '(' {
stack.push(ch);
} else if ch == ')' {
while *stack.last().unwrap() != '(' {
result.push(stack.pop().unwrap());
}
stack.pop();
} else {
while !stack.is_empty() && precedence(*stack.last().unwrap()) >= precedence(ch) {
result.push(stack.pop().unwrap());
}
stack.push(ch);
}
}
while !stack.is_empty() {
result.push(stack.pop().unwrap());
}
result
}
fn main() {
let expr = "(a+b)*c";
let postfix = infix_to_postfix(expr);
println!("{}", postfix);
}Attempts:
2 left
💡 Hint
Parentheses change the order of operations by forcing evaluation inside them first.
✗ Incorrect
Parentheses cause 'a+b' to be evaluated first, so 'ab+' is formed, then multiplied by 'c', resulting in 'ab+c*'.
🔧 Debug
advancedIdentify the Error in Postfix Conversion Code
This Rust function aims to convert infix to postfix but produces incorrect output for
a+b*c. What is the error?DSA Rust
fn infix_to_postfix(expr: &str) -> String { let mut stack: Vec<char> = Vec::new(); let mut result = String::new(); for ch in expr.chars() { if ch.is_alphanumeric() { result.push(ch); } else { while !stack.is_empty() && precedence(ch) <= precedence(*stack.last().unwrap()) { result.push(stack.pop().unwrap()); } stack.push(ch); } } while !stack.is_empty() { result.push(stack.pop().unwrap()); } result }
Attempts:
2 left
💡 Hint
Check the condition that decides when to pop from the stack.
✗ Incorrect
The condition uses precedence(ch) <= precedence(top), which reverses the intended logic. It should pop while the top of stack has higher or equal precedence than the current operator.
🧠 Conceptual
advancedStack Usage in Infix to Postfix Conversion
Why is a stack the ideal data structure for converting infix expressions to postfix notation?
Attempts:
2 left
💡 Hint
Think about how operators are processed in the order they appear and how precedence affects their placement.
✗ Incorrect
A stack lets us push operators and pop them in reverse order, which helps apply operator precedence and associativity rules correctly during conversion.
❓ Predict Output
expertOutput of Complex Infix to Postfix Conversion
What is the output of this Rust code converting the infix expression
a+b*(c^d-e)^(f+g*h)-i to postfix notation?DSA Rust
fn precedence(op: char) -> i32 {
match op {
'+' | '-' => 1,
'*' | '/' => 2,
'^' => 3,
_ => 0,
}
}
fn infix_to_postfix(expr: &str) -> String {
let mut stack: Vec<char> = Vec::new();
let mut result = String::new();
for ch in expr.chars() {
if ch.is_alphanumeric() {
result.push(ch);
} else if ch == '(' {
stack.push(ch);
} else if ch == ')' {
while *stack.last().unwrap() != '(' {
result.push(stack.pop().unwrap());
}
stack.pop();
} else {
while !stack.is_empty() && precedence(*stack.last().unwrap()) >= precedence(ch) {
if ch == '^' && *stack.last().unwrap() == '^' {
break;
}
result.push(stack.pop().unwrap());
}
stack.push(ch);
}
}
while !stack.is_empty() {
result.push(stack.pop().unwrap());
}
result
}
fn main() {
let expr = "a+b*(c^d-e)^(f+g*h)-i";
let postfix = infix_to_postfix(expr);
println!("{}", postfix);
}Attempts:
2 left
💡 Hint
Remember that '^' is right-associative and has highest precedence.
✗ Incorrect
The code correctly handles operator precedence and right-associativity of '^'. The postfix expression is 'abcd^e-fgh*+^*+i-'.
