A circular queue of capacity 5 (indices 0 to 4) starts empty with front = 0 and rear = −1. Five elements are enqueued, two are dequeued and one more is enqueued. What are front and rear now?
Correct answer D. front = 2, rear = 0
Explanation
Five enqueues move rear from −1 to 4. Two dequeues move front from 0 to 2. The next enqueue advances rear to (4 + 1) % 5 = 0, wrapping to the start of the array, which is the slot freed by the first dequeue. The queue now holds four elements. rear = 5 is impossible because indices only run from 0 to 4.
Answer checked How we write and check questions
Discussion