Practice mode Data Structures

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.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.