Data Structures & AlgorithmsVery hardGATE CS · ICPC regional

How many valid sequences of 4 push and 4 pop operations are there on an initially empty stack, where every pop is applied only to a non-empty stack and the stack ends empty?

  1. A. 12
  2. B. 14
  3. C. 16
  4. D. 28

Correct answer: B. 14

Each valid sequence corresponds to a well-formed bracket string, so the answer is the Catalan number C4. 2^4 = 16 is the count if pops were unconstrained by the stack.

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

Exams that ask this topic