Interview Bootcamp
Ask Author
Back to course home

0% completed

Vote For New Content
Engin Yildirim
Queue full condition check could be simpler

Engin Yildirim

Oct 19, 2024

We can do the following to check whether the queue is full or not when it is not empty:

if ((front == (rear + 1) % size)) { System.out.println("Queue is Full"); }

This condition unambiguously handles the wrap-around behaviour of the circular queue and ensures accurate detection of the full condition in all scenarios.

0

0

Comments
Comments
Joseph D
Joseph Da year ago

I agree with your code.

It makes me wonder why is there size - 1 when checking if queue is full.

From the course:

if (self.front == (self.rear + 1) % (self.size - 1)): # Queue is full