Data Structures & AlgorithmsModerateTCS NQT · Accenture

Problem: A stack-based simulation begins with an empty stack. Operations in order: push 4, push 7, push 2, pop, push 9, pop, push 5. What is the sum of the values remaining on the stack, read from bottom to top?

  1. A. 27
  2. B. 25
  3. C. 11
  4. D. 16

Correct answer: D. 16

After push 4, 7, 2, pop (2), push 9, pop (9), push 5 the stack holds 4, 7, 5, sum 16. Ignoring the pops gives 27, missing the second pop gives 25, and missing the final push gives 11.

More Coding Interview Problems questions

Exams that ask this topic