Data Structures & AlgorithmsModerateCognizant · Capgemini pattern

What is the value of the prefix expression − * + 2 3 4 / 8 2 ?

  1. A. 16
  2. B. 4
  3. C. 18
  4. D. -16

Correct answer: A. 16

Reading it as a tree: −( *( +(2, 3), 4 ), /(8, 2) ) = (5 × 4) − 4 = 16. Scanning right to left with a stack, the first popped value is the LEFT operand; swapping them gives 4 − 20 = −16.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic