Data Structures & AlgorithmsModerateTCS NQT · Accenture

Problem: Start with the empty stack. Process the string "7 3 * 2 + 5 -" as postfix tokens (numbers are pushed, operators pop two values, compute second-from-top op top). What is the result?

  1. A. 18
  2. B. 14
  3. C. 23
  4. D. 30

Correct answer: A. 18

Postfix: 7 3 * → 21, 21 2 + → 23, 23 5 - → 18 (second-from-top minus top). Reading it as 21 − (2 + 5) gives 14, stopping before the last operator leaves 23, and doing + before * gives 7 × 5 − 5 = 30.

More Coding Interview Problems questions

Exams that ask this topic