Data Structures & AlgorithmsVery hardGATE CS

The integers 1, 2, 3, 4, 5, 6 are pushed onto an initially empty stack in this order; pops may happen at any time, and the 6 popped values form an output sequence. How many distinct output sequences start with 3?

  1. A. 35
  2. B. 56
  3. C. 28
  4. D. 42

Correct answer: C. 28

Once 1, 2, 3 are pushed and 3 is popped first, 1 and 2 stay on the stack (2 on top) while 4, 5, 6 can still be pushed and popped in any valid interleaving. Enumerating all 132 valid output sequences, exactly 28 start with 3 (those starting with 1 number 42, with 2 number 42).

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

Exams that ask this topic