GATE Computer ScienceVery hardGATE CS (PYQ pattern)
Integers 1, 2, 3, 4, 5 are pushed on an initially empty stack in this order; pops may be interleaved with pushes at any time. How many of the possible output sequences begin with 3?
- A. 11
- B. 14
- C. 9
- D. 7
Correct answer: C. 9
Outputting 3 first forces push 1, 2, 3 and pop 3. The stack now holds 1, 2 (2 on top) and 4, 5 are still to be pushed; counting the valid completions (2 must leave before 1) gives 9, checked by brute force over all 120 orders.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- 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…
- 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…