AI InterviewSearch…Search problems, topics, or pages…Ctrl KGet started
Mediumexpressionconversion

~90s

Infix to postfix

Convert A + B * C - D / E to postfix, with * and / binding tighter than + and - and all operators left associative.

Show the answer and the worked solution

Correct answer: B.

A B C * + D E / -

Worked solution

  1. Precedence groups the expression as (A + (B * C)) - (D / E).
  2. B * C becomes B C *.
  3. A + (B C *) becomes A B C * +.
  4. D / E becomes D E /.
  5. 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.

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.

Question

Convert A + B * C - D / E to postfix, with * and / binding tighter than + and - and all operators left associative.

Options

  1. A B + C * D E / -
  2. A B C * + D E / -
  3. A B C * D E / + -
  4. A B C + * D / E -
Show the answer and the worked solution

Correct answer: B. A B C * + D E / -

Worked solution

  1. Precedence groups the expression as (A + (B * C)) - (D / E).
  2. B * C becomes B C *.
  3. A + (B C *) becomes A B C * +.
  4. D / E becomes D E /.
  5. 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