Question:medium

Which one of the following is the correct way to increment the rear end of a circular queue represented as an array of size M?

Show Hint

The index must wrap from the last slot back to 0. Which formula gives 0 when rear equals M minus 1?
Updated On: Jul 2, 2026
  • rear = rear + 1
  • rear = 1
  • (rear + 1) % M
  • (rear % M) + 1
Show Solution

The Correct Option is C

Solution and Explanation

Idea: Test each formula at the wrap boundary $rear = M-1$.

The circular queue needs the index to cycle through $0, 1, \dots, M-1, 0, \dots$. The advance rule must add one yet stay inside the range.

Check $(rear + 1) \bmod M$ at the last slot:

\[(M-1+1) \bmod M = M \bmod M = 0\]

It wraps to $0$ as required, and for any interior index it simply adds one. The plain $rear+1$ overflows; $rear=1$ is a reset not an increment; and $(rear \bmod M)+1$ can produce $M$, an invalid index.

\[\boxed{(rear + 1)\ \%\ M}\]
Was this answer helpful?
0