A circular queue is a type of queue where the last position is connected back to the first position to make a circle. It helps use the array space efficiently by reusing empty spaces after dequeue operations.
Click to reveal answer
beginner
How do you check if a circular queue is full?
The queue is full if the next position of rear is the front. In formula: (rear + 1) % size == front.
Click to reveal answer
beginner
What happens to front and rear pointers when you enqueue in an empty circular queue?
Both front and rear pointers are set to 0 when the first element is enqueued in an empty circular queue.
Click to reveal answer
intermediate
Explain the dequeue operation in a circular queue.
Dequeue removes the element at front. If front equals rear after removal, both are reset to -1 (empty queue). Otherwise, front moves to (front + 1) % size.
Click to reveal answer
intermediate
Why is a circular queue better than a simple queue using arrays?
A circular queue reuses empty spaces freed by dequeue operations, avoiding wasted space and the need to shift elements, unlike a simple queue which can become full even if there is space at the front.
Click to reveal answer
What is the initial value of front and rear in an empty circular queue?
A1
B0
Csize - 1
D-1
✗ Incorrect
Both front and rear are set to -1 to indicate the queue is empty.
Which condition indicates the circular queue is full?
A(rear + 1) % size == front
Bfront == rear
Cfront == -1
Drear == size - 1
✗ Incorrect
When the next position of rear is front, the queue is full.
After dequeuing the last element, what happens to front and rear?
Afront = rear = 0
Bfront = rear = -1
Cfront = 0, rear = -1
Dfront = -1, rear = 0
✗ Incorrect
Both pointers reset to -1 to mark the queue as empty.
What is the purpose of using modulo (%) in circular queue operations?
ATo check if queue is empty
BTo increment the queue size
CTo wrap around the array indices
DTo find the size of the queue
✗ Incorrect
Modulo helps wrap the index to the start when it reaches the end of the array.
Which operation moves the rear pointer in a circular queue?
AEnqueue
BPeek
CDequeue
DIsEmpty
✗ Incorrect
Enqueue adds an element and moves the rear pointer.
Describe how enqueue and dequeue operations work in a circular queue using an array.
Think about how front and rear move and how the array wraps around.
You got /4 concepts.
Explain why a circular queue is more efficient than a simple queue implemented with an array.
Consider what happens when elements are removed from the front.
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 eliminates the need for front and rear pointers
B. It allows storing different data types in the same queue
C. It automatically sorts the elements in the queue
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 rear reaches the end, no more elements can be added even if there are empty slots at the front.
Step 2: Circular queue advantage
Circular queue connects the end of the array back to the front, allowing reuse of empty slots freed by dequeuing.
Final Answer:
It efficiently uses the array space by reusing freed slots -> Option D
Quick Check:
Space reuse = Advantage [OK]
Hint: Circular queue reuses space freed by dequeued elements [OK]
Common Mistakes:
Thinking circular queue sorts elements
Believing circular queue removes need for pointers
Assuming it supports multiple data types inherently
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 pointers?
easy
A. front == (rear + 1) % n
B. rear == n - 1
C. front == rear
D. rear == (front + 1) % n
Solution
Step 1: Understand full condition in circular queue
In circular queue, queue is full when next position of rear is front, i.e., (rear + 1) modulo n equals front.
Step 2: Match condition with options
front == (rear + 1) % n matches this condition: front == (rear + 1) % n.
Final Answer:
front == (rear + 1) % n -> Option A
Quick Check:
Full condition = front == (rear + 1) % n [OK]
Hint: Full when front is next to rear in circular manner [OK]
Common Mistakes:
Using rear == n - 1 which is for linear queue
Confusing front == rear as full (actually empty)
Swapping front and rear in 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); dequeue(); enqueue(60); What will be the values of front and rear?
front = 2, rear = 0 which corresponds to front = 2, rear = 0.
Final Answer:
front = 2, rear = 0 -> Option B
Quick Check:
Trace pointers carefully [OK]
Hint: Track front and rear after each enqueue/dequeue [OK]
Common Mistakes:
Not updating rear circularly after last index
Confusing front and rear updates on dequeue
Ignoring initial front = -1 condition
4. The following code snippet is part of a circular queue implementation. Identify the bug:
void enqueue(int value) {
if ((rear + 1) % size == front) {
cout << "Queue is full" << endl;
return;
}
if (front == -1) front = 0;
rear = (rear + 1) % size;
arr[rear] = value;
}
medium
A. No bug, code is correct
B. The front pointer should be updated after rear
C. The initial check for empty queue should be before full check
D. The condition to check full queue is incorrect
Solution
Step 1: Analyze full queue condition
The condition (rear + 1) % size == front correctly checks if queue is full in circular queue.
Step 2: Check front pointer update and enqueue steps
If queue is empty (front == -1), front is set to 0 before inserting. rear is updated circularly and value inserted at arr[rear].
Final Answer:
No bug, code is correct -> Option A
Quick Check:
Full check and pointers update are correct [OK]
Hint: Full check uses modulo; front updated only if empty [OK]
Common Mistakes:
Misplacing front update after rear
Incorrect full condition without modulo
Checking empty after full condition
5. You have a circular queue of size 6. After performing the following operations: enqueue(5); enqueue(10); enqueue(15); dequeue(); dequeue(); enqueue(20); enqueue(25); enqueue(30); enqueue(35); What will be the sequence of elements in the queue from front to rear?