Data Structures & AlgorithmsModerateCodeforces Div2 B-D · ICPC regional

Numbers 1, 2, 3, 4 are pushed in this order onto a stack. Can the pop sequence 3, 4, 2, 1 be produced?

  1. A. yes
  2. B. no, because 2 must be popped before 3
  3. C. yes, but only with a second stack
  4. D. no, because the stack holds at most two items

Correct answer: A. yes

Push 1, 2 and 3 and pop 3; push 4 and pop 4; the stack top is then 2, and 2 then 1 come off in order. The sequence is valid.

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

Exams that ask this topic