Data Structures & AlgorithmsVery hardGATE CS (PYQ pattern)

A circular queue uses an array of size 5 with the convention that front is the index of the next element to dequeue, rear is the index of the next enqueue, and the queue is full when (rear + 1) mod 5 = front. Starting empty: enqueue 1, 2, 3, 4, 5, dequeue, dequeue, enqueue 6, 7, 8, dequeue, enqueue 9, dequeue. Which enqueues are rejected, and what are the final front and rear?

  1. A. Rejected 5 and 8; front = 4, rear = 2
  2. B. Rejected 8 only; front = 4, rear = 3
  3. C. Rejected 5 and 8; front = 3, rear = 2
  4. D. Rejected 5 only; front = 4, rear = 3

Correct answer: A. Rejected 5 and 8; front = 4, rear = 2

This convention wastes one slot, so only 4 elements fit: 5 is rejected at once. After two dequeues 6 and 7 fit, making it full again, so 8 is rejected. Two more dequeues remove 3 and 4, leaving 6, 7 and 9 with front = 4 and rear = 2.

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

Exams that ask this topic