Challenge - 5 Problems
Circular Queue Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Circular Queue Enqueue and Dequeue Operations
What is the printed state of the circular queue after the following operations?
Initialize queue with size 5.
Enqueue 10, 20, 30.
Dequeue one element.
Enqueue 40, 50, 60.
Print queue contents from front to rear.
Initialize queue with size 5.
Enqueue 10, 20, 30.
Dequeue one element.
Enqueue 40, 50, 60.
Print queue contents from front to rear.
DSA Java
class CircularQueue { int[] arr; int front, rear, size, capacity; CircularQueue(int capacity) { this.capacity = capacity; arr = new int[capacity]; front = 0; size = 0; rear = capacity - 1; } void enqueue(int item) { if (size == capacity) return; rear = (rear + 1) % capacity; arr[rear] = item; size++; } int dequeue() { if (size == 0) return -1; int item = arr[front]; front = (front + 1) % capacity; size--; return item; } void printQueue() { int count = size; int i = front; while (count > 0) { System.out.print(arr[i] + " -> "); i = (i + 1) % capacity; count--; } System.out.print("null\n"); } } public class Main { public static void main(String[] args) { CircularQueue q = new CircularQueue(5); q.enqueue(10); q.enqueue(20); q.enqueue(30); q.dequeue(); q.enqueue(40); q.enqueue(50); q.enqueue(60); q.printQueue(); } }
Attempts:
2 left
💡 Hint
Remember that dequeue removes the front element and enqueue adds at the rear in a circular manner.
✗ Incorrect
After enqueueing 10, 20, 30, the queue is [10, 20, 30]. Dequeue removes 10. Then enqueue 40, 50, 60 fills the queue to [20, 30, 40, 50, 60]. Printing from front to rear shows these elements in order.
🧠 Conceptual
intermediateUnderstanding Circular Queue Full Condition
In a circular queue implemented using an array, which condition correctly checks if the queue is full?
Attempts:
2 left
💡 Hint
Think about how the rear and front pointers move in a circular manner.
✗ Incorrect
The queue is full when the next position of rear (circularly) is front. This means (rear + 1) % capacity == front.
🔧 Debug
advancedIdentify the Bug in Circular Queue Dequeue Method
What error will occur when running this dequeue method if the queue is empty?
DSA Java
int dequeue() { if (size == 0) return -1; int item = arr[front]; front = (front + 1) % capacity; size--; return item; }
Attempts:
2 left
💡 Hint
Check what happens when size is zero.
✗ Incorrect
The method checks if size is zero and returns -1 immediately, so no exception occurs.
❓ Predict Output
advancedResult of Circular Queue After Wrap-Around Enqueue
Given a circular queue of capacity 4, after these operations:
enqueue(1), enqueue(2), enqueue(3), dequeue(), dequeue(), enqueue(4), enqueue(5), enqueue(6)
What is the queue content printed from front to rear?
enqueue(1), enqueue(2), enqueue(3), dequeue(), dequeue(), enqueue(4), enqueue(5), enqueue(6)
What is the queue content printed from front to rear?
DSA Java
class CircularQueue { int[] arr; int front, rear, size, capacity; CircularQueue(int capacity) { this.capacity = capacity; arr = new int[capacity]; front = 0; size = 0; rear = capacity - 1; } void enqueue(int item) { if (size == capacity) return; rear = (rear + 1) % capacity; arr[rear] = item; size++; } int dequeue() { if (size == 0) return -1; int item = arr[front]; front = (front + 1) % capacity; size--; return item; } void printQueue() { int count = size; int i = front; while (count > 0) { System.out.print(arr[i] + " -> "); i = (i + 1) % capacity; count--; } System.out.print("null\n"); } } public class Main { public static void main(String[] args) { CircularQueue q = new CircularQueue(4); q.enqueue(1); q.enqueue(2); q.enqueue(3); q.dequeue(); q.dequeue(); q.enqueue(4); q.enqueue(5); q.enqueue(6); q.printQueue(); } }
Attempts:
2 left
💡 Hint
Remember the queue capacity and how front and rear wrap around.
✗ Incorrect
After enqueuing 1,2,3 and dequeuing twice, one element (3) remains at front (index 2, size=1). Enqueuing 4 (rear=3), 5 (rear=0), 6 (rear=1) fills to size=4. Printing from front=2: 3 -> 4 -> 5 -> 6 -> null.
🧠 Conceptual
expertWhy Use Circular Queue Instead of Simple Queue?
Which is the main advantage of using a circular queue over a simple linear queue implemented with an array?
Attempts:
2 left
💡 Hint
Think about what happens when elements are dequeued in a simple array queue.
✗ Incorrect
A circular queue reuses freed spaces by wrapping around, avoiding costly shifting of elements required in a simple linear queue.
