GATE Computer ScienceHardGATE CS (PYQ pattern)

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 (number of items); an item is enqueued at index (front + count) % 8, a dequeue advances front by 1 mod 8, and all 8 slots can be used. An enqueue on a full queue or a dequeue on an empty one fails and changes nothing. Starting from front = 6 and count = 0, the operations E E E E E D D E E E E E E D E E D D D E are applied (E = enqueue, D = dequeue). How many operations fail, what is front at the end, and at which index was the last item stored?

  1. A. 2 failed, front = 4, last item at index 1
  2. B. 3 failed, front = 4, last item at index 0
  3. C. 2 failed, front = 4, last item at index 2
  4. D. 3 failed, front = 3, last item at index 1

Correct answer: A. 2 failed, front = 4, last item at index 1

Track (front, count): 5 enqueues give count 5; 2 dequeues move front to 0 with count 3; the next 6 enqueues fill the array (count 8) after 5 of them, so the 6th fails; D, E, E: front 1, count 8 again after one E and the second E fails; D D D give front 4, count 5; the final E goes to (4 + 5) % 8 = 1. In total 2 failures, front = 4, last index 1. Treating the queue as having only 7 usable slots gives 3 failed, front = 4, last item at index 0.

More Programming and Data Structures (GATE) questions

Exams that ask this topic