GATE Computer ScienceModerateGATE CS (PYQ pattern)
A queue holds 5, 12, 7, 3, 9, 14, 2, 8 (front first). To reverse the first 5 elements, they are dequeued onto a stack, popped back and enqueued at the rear, and then the remaining elements are each moved (dequeue + enqueue) from the front to the rear. How many enqueue operations are made in total, and what is the final queue from the front?
- A. 5 enqueues; 9 3 7 12 5 14 2 8
- B. 8 enqueues; 9 3 7 12 5 14 2 8
- C. 8 enqueues; 5 12 7 3 9 14 2 8
- D. 8 enqueues; 9 3 7 12 5 8 2 14
Correct answer: B. 8 enqueues; 9 3 7 12 5 14 2 8
The 5 popped items go to the rear in reverse order (9, 3, 7, 12, 5) behind 14, 2, 8; rotating those three behind them restores the tail order, so the final queue is 9 3 7 12 5 14 2 8. Total enqueues 5 + 3 = 8.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- In the binary tree (key, left, right) = (44, (20, (12), (8)), (24, (9, (4), (5)), (15, -, (15)))), how many non-leaf nodes have a key…
- int ds(int n) { return n < 10 ? n : ds(n / 10) + n % 10; } int steps(int n) { int c = 0; while (n >= 10) { n = ds(n); c++; } return c; }…
- The keys 3, 9, 6, 12, 1, 15, 8 are inserted one after another into an initially empty max-heap (each insertion places the key at the end…
- int fib(int n) { if (n < 2) return n; return fib(n-1) + fib(n-2); } Including the initial call, how many times is fib executed in total…
- A hash table of size 13 uses double hashing with h1(k) = k mod 13, h2(k) = 1 + (k mod 11) and probe sequence (h1 + i·h2) mod 13. The keys…
- A dynamic array starts with capacity 1. Whenever an append finds it full, a new array of double the capacity is allocated and all current…