Challenge - 5 Problems
Postfix Expression Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this Rust code evaluating a postfix expression?
Given the postfix expression "231*+9-", what is the output printed by the Rust program?
DSA Rust
fn evaluate_postfix(expr: &str) -> i32 { let mut stack = Vec::new(); for ch in expr.chars() { if ch.is_digit(10) { stack.push(ch.to_digit(10).unwrap() as i32); } else { let b = stack.pop().unwrap(); let a = stack.pop().unwrap(); let res = match ch { '+' => a + b, '-' => a - b, '*' => a * b, '/' => a / b, _ => 0, }; stack.push(res); } } stack.pop().unwrap() } fn main() { let expr = "231*+9-"; let result = evaluate_postfix(expr); println!("{}", result); }
Attempts:
2 left
💡 Hint
Trace the stack operations step-by-step for each character in the expression.
✗ Incorrect
The expression evaluates as follows: push 2, push 3, push 1, multiply top two (3*1=3), stack is [2,3], add top two (2+3=5), stack is [5], push 9, subtract top two (5-9=-4). Final result is -4.
❓ Predict Output
intermediateWhat is the output of this Rust code evaluating a postfix expression with division?
Given the postfix expression "82/3-", what is the output printed by the Rust program?
DSA Rust
fn evaluate_postfix(expr: &str) -> i32 { let mut stack = Vec::new(); for ch in expr.chars() { if ch.is_digit(10) { stack.push(ch.to_digit(10).unwrap() as i32); } else { let b = stack.pop().unwrap(); let a = stack.pop().unwrap(); let res = match ch { '+' => a + b, '-' => a - b, '*' => a * b, '/' => a / b, _ => 0, }; stack.push(res); } } stack.pop().unwrap() } fn main() { let expr = "82/3-"; let result = evaluate_postfix(expr); println!("{}", result); }
Attempts:
2 left
💡 Hint
Remember integer division truncates towards zero in Rust.
✗ Incorrect
The expression evaluates as: push 8, push 2, divide (8/2=4), push 3, subtract (4-3=1). Final result is 1.
🔧 Debug
advancedWhat error does this Rust code produce when evaluating a postfix expression?
Consider the Rust code below. What error will occur when running it with input "34+5*"?
DSA Rust
fn evaluate_postfix(expr: &str) -> i32 { let mut stack = Vec::new(); for ch in expr.chars() { if ch.is_digit(10) { stack.push(ch.to_digit(10).unwrap() as i32); } else { let b = stack.pop().unwrap(); let a = stack.pop().unwrap(); let res = match ch { '+' => a + b, '-' => a - b, '*' => a * b, '/' => a / b, _ => 0, }; stack.push(res); } } stack.pop().unwrap() } fn main() { let expr = "34+5*"; let result = evaluate_postfix(expr); println!("{}", result); }
Attempts:
2 left
💡 Hint
Check if the stack has enough operands before popping.
✗ Incorrect
The expression "34+5*" evaluates as (3+4)*5 = 7*5 = 35. The code runs successfully without errors.
❓ Predict Output
advancedWhat is the output of this Rust code evaluating a postfix expression with multiple operators?
Given the postfix expression "562+*84/-", what is the output printed by the Rust program?
DSA Rust
fn evaluate_postfix(expr: &str) -> i32 { let mut stack = Vec::new(); for ch in expr.chars() { if ch.is_digit(10) { stack.push(ch.to_digit(10).unwrap() as i32); } else { let b = stack.pop().unwrap(); let a = stack.pop().unwrap(); let res = match ch { '+' => a + b, '-' => a - b, '*' => a * b, '/' => a / b, _ => 0, }; stack.push(res); } } stack.pop().unwrap() } fn main() { let expr = "562+*84/-"; let result = evaluate_postfix(expr); println!("{}", result); }
Attempts:
2 left
💡 Hint
Evaluate step-by-step: push numbers, then apply operators in order.
✗ Incorrect
The expression evaluates as follows: push 5, push 6, push 2, add top two (6+2=8), stack is [5,8], multiply top two (5*8=40), stack is [40], push 8, push 4, divide top two (8/4=2), stack is [40,2], subtract top two (40-2=38). Final result is 38.
🧠 Conceptual
expertHow many items remain in the stack after evaluating the postfix expression "12+34+*"?
Consider the postfix expression "12+34+*" evaluated using a stack. How many items remain in the stack after the entire expression is processed?
Attempts:
2 left
💡 Hint
Each operator pops two items and pushes one result. Count pushes and pops carefully.
✗ Incorrect
The expression evaluates as follows:
- push 1, push 2
- '+' => pop 2 and 1, push 3
- push 3, push 4
- '+' => pop 4 and 3, push 7
- '*' => pop 7 and 3, push 21
At the end, only one item (21) remains in the stack.
