Challenge - 5 Problems
Circular Queue Master
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 4.
Enqueue 10, 20, 30.
Dequeue one element.
Enqueue 40, 50.
Print queue contents from front to rear.
Initialize queue with size 4.
Enqueue 10, 20, 30.
Dequeue one element.
Enqueue 40, 50.
Print queue contents from front to rear.
DSA Go
package main import "fmt" func main() { size := 4 queue := make([]int, size) front, rear, count := 0, -1, 0 enqueue := func(val int) bool { if count == size { return false } rear = (rear + 1) % size queue[rear] = val count++ return true } dequeue := func() (int, bool) { if count == 0 { return 0, false } val := queue[front] front = (front + 1) % size count-- return val, true } enqueue(10) enqueue(20) enqueue(30) dequeue() enqueue(40) enqueue(50) // Print queue contents for i := 0; i < count; i++ { idx := (front + i) % size fmt.Print(queue[idx]) if i != count-1 { fmt.Print(" -> ") } } fmt.Print(" -> null") }
Attempts:
2 left
💡 Hint
Remember that dequeue removes the front element and enqueue adds at rear with wrap-around.
✗ Incorrect
After enqueueing 10,20,30 queue is [10,20,30]. Dequeue removes 10. Then enqueue 40 and 50 fills the queue with 20,30,40,50 in order with wrap-around.
❓ Predict Output
intermediateResult of Circular Queue Full Condition Check
Given a circular queue of size 5, after these operations:
Enqueue 1, 2, 3, 4, 5
Dequeue two elements
Enqueue 6, 7
What will be the value of 'count' and will the queue be full?
Enqueue 1, 2, 3, 4, 5
Dequeue two elements
Enqueue 6, 7
What will be the value of 'count' and will the queue be full?
DSA Go
package main import "fmt" func main() { size := 5 queue := make([]int, size) front, rear, count := 0, -1, 0 enqueue := func(val int) bool { if count == size { return false } rear = (rear + 1) % size queue[rear] = val count++ return true } dequeue := func() (int, bool) { if count == 0 { return 0, false } val := queue[front] front = (front + 1) % size count-- return val, true } enqueue(1) enqueue(2) enqueue(3) enqueue(4) enqueue(5) dequeue() dequeue() enqueue(6) enqueue(7) fmt.Printf("count=%d, full=%t", count, count == size) }
Attempts:
2 left
💡 Hint
Count increases on enqueue and decreases on dequeue. Full means count equals size.
✗ Incorrect
After enqueueing 5 elements, count=5. Dequeue twice reduces count to 3. Enqueue twice increases count to 5 again. But since size is 5, count=5 means full=true. But careful: enqueue(6) and enqueue(7) after two dequeues fills count to 5, so full is true.
🔧 Debug
advancedIdentify the Bug in Circular Queue Dequeue Function
What error will occur when running this dequeue function in a circular queue implementation if the queue is empty?
DSA Go
func dequeue() int { if count == 0 { return -1 } val := queue[front] front = (front + 1) % size count-- return val }
Attempts:
2 left
💡 Hint
Check what happens when count is zero and dequeue is called.
✗ Incorrect
The function returns -1 explicitly when count is zero, so no panic or runtime error occurs.
🚀 Application
advancedChoosing Circular Queue Over Linear Queue
Why is a circular queue preferred over a linear queue when implemented using arrays?
Attempts:
2 left
💡 Hint
Think about what happens to array space after many dequeues in linear queue.
✗ Incorrect
Linear queue wastes space at front after dequeues; circular queue reuses that space by wrapping rear to front.
🧠 Conceptual
expertMaximum Number of Elements in Circular Queue
If a circular queue is implemented using an array of size N, what is the maximum number of elements it can hold without ambiguity in front and rear pointers?
Attempts:
2 left
💡 Hint
One slot is kept empty to distinguish full and empty states.
✗ Incorrect
To differentiate full and empty states, circular queue keeps one slot empty, so max elements is N-1.
