A circular queue is a type of queue where the last position is connected back to the first position to make a circle. It allows efficient use of space by reusing empty slots after dequeue operations.
Click to reveal answer
beginner
How do you check if a circular queue is full?
The circular queue is full when the next position of rear is equal to front. In formula: (rear + 1) % size == front.
Click to reveal answer
intermediate
What happens to front and rear pointers when you enqueue an element in a circular queue?
When enqueueing, rear moves forward by one position using (rear + 1) % size. If the queue was empty, front is set to 0.
Click to reveal answer
intermediate
How do you dequeue an element from a circular queue?
Remove the element at front, then move front forward by one using (front + 1) % size. If after dequeue front becomes equal to rear + 1 (mod size), reset both to -1 (empty queue).
Click to reveal answer
beginner
Why is a circular queue better than a simple linear queue using arrays?
A circular queue reuses empty spaces freed by dequeued elements, avoiding wasted space and the need to shift elements. A linear queue wastes space once rear reaches the end.
Click to reveal answer
What condition indicates that a circular queue is empty?
Afront == -1 and rear == -1
Bfront == rear
C(rear + 1) % size == front
Drear == size - 1
✗ Incorrect
The queue is empty when both front and rear are -1.
If the queue size is 5, front is 3, and rear is 1, is the queue full?
ANo, because rear < front
BYes, because (rear + 1) % size == front
CNo, because front != rear
DYes, because rear == size - 1
✗ Incorrect
Calculate (1 + 1) % 5 = 2, which is not equal to front (3), so queue is not full. Correct answer is No, so option A is wrong. Correct answer is C.
What is the formula to move rear pointer forward in a circular queue?
Arear = rear + 1
Brear = rear - 1
Crear = front + 1
Drear = (rear + 1) % size
✗ Incorrect
Rear moves forward circularly using modulo operation.
After dequeuing the last element, what should front and rear be set to?
Afront = rear = 0
Bfront = 0, rear = size - 1
Cfront = rear = -1
Dfront = rear = size
✗ Incorrect
Setting both to -1 indicates the queue is empty.
Why do we use modulo (%) operator in circular queue?
ATo reset pointers to zero when they reach the end
BTo check if queue is empty
CTo increase queue size
DTo sort the queue elements
✗ Incorrect
Modulo helps wrap pointers back to start to form a circle.
Explain how enqueue and dequeue operations work in a circular queue using an array.
Think about how the pointers move in a circle.
You got /4 concepts.
Describe the advantages of using a circular queue over a simple linear queue implemented with arrays.
Consider what happens when the rear reaches the end of the array.
You got /4 concepts.
Practice
(1/5)
1. What is the main advantage of using a circular queue over a simple linear queue when implemented using an array?
easy
A. It automatically sorts the elements in the queue
B. It allows storing different data types in the same queue
C. It increases the maximum size of the queue beyond the array length
D. It efficiently uses the array space by reusing freed slots
Solution
Step 1: Understand linear queue limitation
In a linear queue using an array, once the rear reaches the end, no more elements can be added even if there are empty slots at the front.
Step 2: Circular queue advantage
A circular queue connects the end of the array back to the front, allowing reuse of empty slots freed by dequeuing, thus using space efficiently.
Final Answer:
It efficiently uses the array space by reusing freed slots -> Option D
Quick Check:
Circular queue reuses space = Advantage [OK]
Hint: Circular queue reuses space freed by dequeue [OK]
Common Mistakes:
Thinking circular queue increases array size
Believing it sorts elements automatically
Confusing data type flexibility with queue type
2. Which of the following is the correct condition to check if a circular queue is full when using an array of size n with front and rear indices?
easy
A. rear == (front + 1) % n
B. front == rear
C. front == (rear + 1) % n
D. rear == n - 1
Solution
Step 1: Recall circular queue full condition
The queue is full when the next position of rear (circularly) is front, meaning no space to insert new element.
Step 2: Express condition mathematically
This condition is: front == (rear + 1) % n, where % is modulo operator to wrap around.
Final Answer:
front == (rear + 1) % n -> Option C
Quick Check:
Full if front is next to rear circularly = Condition B [OK]
Hint: Full when front is just after rear modulo array size [OK]
Common Mistakes:
Using rear == (front + 1) % n (wrong direction)
Checking front == rear (means empty, not full)
Checking rear == n - 1 (linear queue condition)
3. Consider a circular queue of size 5 (indices 0 to 4). Initially, front = -1 and rear = -1. After the following operations: enqueue(10), enqueue(20), enqueue(30), dequeue(), enqueue(40), enqueue(50), enqueue(60) What will be the values of front and rear?
Hint: Track front/rear step-by-step with wrap-around [OK]
Common Mistakes:
Not updating front after dequeue
Forgetting rear wraps to 0 after reaching end
Mixing front and rear indices
4. Given the following code snippet for dequeue operation in a circular queue, identify the error:
int dequeue() {
if (front == -1) {
printf("Queue is empty\n");
return -1;
}
int data = queue[front];
if (front == rear) {
front = rear = -1;
} else {
front = front + 1;
}
return data;
}
medium
A. front should be updated as (front + 1) % size to wrap around
B. rear should be incremented instead of front
C. The check for empty queue is incorrect
D. Data should be returned after updating front
Solution
Step 1: Understand circular queue front update
In circular queue, front index must wrap around using modulo when incremented to stay within array bounds.
Step 2: Identify error in front update
Code uses front = front + 1; which can go beyond array size. Correct is front = (front + 1) % size.
Final Answer:
front should be updated as (front + 1) % size to wrap around -> Option A
Quick Check:
Use modulo for front increment in circular queue [OK]
Hint: Use modulo when incrementing front or rear [OK]
Common Mistakes:
Incrementing front without modulo wrap
Confusing front and rear updates
Returning data before updating front
5. You have a circular queue of size 6. After performing these operations: enqueue(5), enqueue(10), enqueue(15), dequeue(), dequeue(), enqueue(20), enqueue(25), enqueue(30), enqueue(35) What will be the queue content from front to rear?