ProgrammingHardTCS NQT · Accenture pattern

What is the final content of the stack (bottom to top, left to right) after this sequence of operations on an initially empty stack?

What is the final content of the stack (bottom to top, left to right) after this sequence of operations on an initially empty stack?
push 87
push 11
pop
push 58
push 74
push 97
push 9
push 5
push 95
  1. A. 87, 58, 74, 97, 9, 5
  2. B. 11, 58, 74, 97, 9, 5, 95
  3. C. 95, 5, 9, 97, 74, 58, 87
  4. D. 87, 58, 74, 97, 9, 5, 95

Correct answer: D. 87, 58, 74, 97, 9, 5, 95

Tracing push/pop on an empty stack in order gives, bottom-to-top, [87, 58, 74, 97, 9, 5, 95]. Reading top-to-bottom instead gives the reversed trap [95, 5, 9, 97, 74, 58, 87]; mistakenly treating "pop" as removing the front (as in a queue) gives [11, 58, 74, 97, 9, 5, 95].

More Pseudocode and Code Tracing questions

Exams that ask this topic