GATE Computer ScienceVery hardGATE CS (PYQ pattern)
The integers 1 to 6 are pushed in this order onto a stack that can hold at most 2 elements at any moment; pops may be interleaved with pushes at any time, and every number is eventually popped. How many different output sequences are possible?
- A. 32
- B. 132
- C. 89
- D. 21
Correct answer: A. 32
Write a push as U and a pop as D. Each output sequence corresponds to exactly one push/pop word of length 12 that never goes below zero and, here, never above height 2. Such a word splits into returns to height 0; each block is U (UD)^m D with m >= 0, so a block of semilength j is unique, and the words are the compositions of 6: 2^5 = 32 (brute force agrees). Without the limit the count would be C6 = 132; with capacity 3 it would be 89.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- A circular queue stores items in an array of 8 slots (indices 0 to 7) and keeps two fields, front (index of the first item) and count…
- The infix expression a + b (c ^ d - e) ^ (f + g h) - i (with ^ right-associative and higher than and +) is converted to postfix with the…
- Evaluate the postfix expression 9 3 - 4 2 5 - 7 3 / + where / is integer division truncating toward zero.
- Ten people numbered 1 to 10 stand in a circle (a circular linked list in increasing order). Counting starts at person s, who is counted as…
- A singly linked list has 17 nodes numbered 0 to 16 from the head; node 16 points back to node 7 (so the tail of length 7 leads into a…
- A routine rev(k) reverses every block of k consecutive nodes of a singly linked list in place, leaving a final block of fewer than k nodes…