Data Structures & AlgorithmsModerateTCS NQT · Accenture pattern

A circular queue uses an array of size 5 (indices 0–4); front is the index of the first element and rear is the index of the last element. If front = 3 and rear = 1 and the queue is not empty, how many elements does it hold?

  1. A. 4
  2. B. 3
  3. C. 2
  4. D. 5

Correct answer: A. 4

The elements occupy 3, 4, 0, 1 (wrapping past the end): 4 elements. Formula: (rear − front + N) mod N + 1 = (1 − 3 + 5) mod 5 + 1 = 4. Using rear − front = −2 or forgetting the +1 gives the other values.

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

Exams that ask this topic