Infix to postfix — Aptitude Question
Infix to postfix: Convert A + B * C - D / E to postfix, with * and / binding tighter than + and - and all operators… Answer and worked solution on CodeKairo.
- Topic: Data Structures & Algorithms
- Section: Programming MCQs
- Difficulty: Medium
- Time target: 90 seconds
Question
Convert A + B * C - D / E to postfix, with * and / binding tighter than + and - and all operators left associative.
Options
- A B + C * D E / -
- A B C * + D E / -
- A B C * D E / + -
- A B C + * D / E -
Show the answer and the worked solution
Correct answer: B. A B C * + D E / -
Worked solution
- Precedence groups the expression as
(A + (B * C)) - (D / E). B * CbecomesB C *.A + (B C *)becomesA B C * +.D / EbecomesD E /.- The outer subtraction places its two postfix operands first and the operator last, giving
A B C * + D E / -.
Answer: **A B C * + D E / -**.
The idea
Fully parenthesising by precedence turns conversion into mechanical work: each bracket becomes operand, operand, operator. It is faster and safer than running the shunting-yard algorithm in your head.
Shortcut. The first operator in the string must be (B C ), which rules out "A B + C D E / -" and "A B C + D / E -"; + must follow it before D appears, because A + B × C is the whole left operand of −.
Common trap. "A B C * D E / + -" holds every lower-precedence operator to the end, and so evaluates A − (B × C + D ÷ E), a different expression.
All Data Structures & Algorithms questions · placement mock tests