Data Structures & AlgorithmsModerateWipro · Capgemini pattern

Starting from an empty stack: push 5, push 3, pop, push 8, push 2, pop, pop, push 7, push 1. What are the stack contents from top to bottom?

  1. A. 1, 7, 8, 5
  2. B. 5, 7, 1
  3. C. 1, 7, 2, 5
  4. D. 1, 7, 5

Correct answer: D. 1, 7, 5

After push 5, 3 and pop: [5]. Push 8, 2 → [5, 8, 2]; two pops remove 2 then 8 → [5]. Push 7, 1 → [5, 7, 1]; top first: 1, 7, 5. '5, 7, 1' lists bottom to top.

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

Exams that ask this topic