Data Structures & AlgorithmsHard

How many different valid sequences of PUSH and POP operations are there for a stack that performs exactly 6 pushes and 6 pops in total, such that the stack is never popped when empty and ends empty?

  1. A. 2048
  2. B. 69
  3. C. 42
  4. D. 132

Correct answer: D. 132

Each valid sequence is a balanced Dyck word, so the count is the Catalan number C(n) with n = 6, which is 132. Counting all 2^(2n) operation strings ignores the emptiness constraint, and the Catalan number for n−1 is a different instance.

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

Exams that ask this topic