Data Structures and Algorithms

The expression “3 4 + 2 * 5 −” in reverse Polish notation (postfix notation) is evaluated by pushing each number onto a stack and, for each operator, popping two values, computing the result and pushing it back. What is the result? The left operand of an operator is the value that was pushed earlier.

  1. A−9
  2. B6
  3. C9
Show answer and explanation

Answer: C. 9

Push 3 and 4, then + pops them and pushes 3 + 4 = 7. Push 2, then * pushes 7 × 2 = 14. Push 5, then − subtracts the later value 5 from the earlier value 14, giving 14 − 5 = 9. In postfix notation the order of operations is fixed by the sequence itself, so no parentheses are needed; this expression corresponds to (3 + 4) × 2 − 5 in ordinary notation. −9 results from swapping the operands of the subtraction, and 6 from reading it as 3 + 4 × 2 − 5 without parentheses.

  • A. This swaps the operands of the subtraction (5 − 14). The left operand is 14, which was pushed earlier.
  • B. This computes 3 + 4 × 2 − 5 without parentheses. In postfix notation the + is done first.
  • C. Correct. (3 + 4) × 2 − 5 = 9.
Practice this topic Back to topic

Related calculators

Books on Data Structures and Algorithms Ad

As an Amazon Associate I earn from qualifying purchases.

More Data Structures and Algorithms questions

  1. Which is the correct description of a collision in a hash table?
  2. Keys 10, 17, 24 and 3 are stored in this order in a hash table of size 7 (positions 0 to 6) with the hash function h(k) = k mod 7. Collisions are resolved by linear probing, which moves forward one position at a time (wrapping from 6 to 0) to find a free slot. At which position is key 3 stored?
  3. Which correctly compares chaining (linking the elements at the same position into a linked list) and open addressing (finding another free position within the table) as ways to resolve hash table collisions? The load factor is the number of stored elements divided by the table size.
  4. Which is a prerequisite for using binary search, which compares the target with the middle element and halves the search range each time?
  5. A binary search is performed on 1,000 distinct values sorted in ascending order. If one comparison tells you whether the values are equal, smaller or larger, what is the maximum number of comparisons, counting both successful and unsuccessful searches?