Data Structures & AlgorithmsVery hard

Starting from an empty stack, perform in order: push 4, push 9, pop, push 2, push 7, pop, pop, push 5. Reading from the bottom to the top, what is the contents of the stack at the end?

  1. A. 5 4
  2. B. 4 5
  3. C. 4 2 5
  4. D. 9 5

Correct answer: B. 4 5

Track the stack contents exactly: pops remove the most recent survivor, so the final contents from bottom to top are determined by the surviving pushes. Reversing the reading order or dropping the last push gives a different sequence.

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

Exams that ask this topic