Challenge - 5 Problems
Queue Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Enqueue and Dequeue Operations
What is the printed state of the queue after these operations?
DSA Go
package main import "fmt" type Queue struct { items []int front int rear int size int capacity int } func NewQueue(capacity int) *Queue { return &Queue{ items: make([]int, capacity), front: 0, rear: -1, size: 0, capacity: capacity, } } func (q *Queue) Enqueue(val int) bool { if q.size == q.capacity { return false } q.rear = (q.rear + 1) % q.capacity q.items[q.rear] = val q.size++ return true } func (q *Queue) Dequeue() (int, bool) { if q.size == 0 { return 0, false } val := q.items[q.front] q.front = (q.front + 1) % q.capacity q.size-- return val, true } func (q *Queue) Print() { fmt.Print("Queue: ") for i := 0; i < q.size; i++ { index := (q.front + i) % q.capacity fmt.Printf("%d", q.items[index]) if i != q.size-1 { fmt.Print(" -> ") } } fmt.Println(" -> null") } func main() { q := NewQueue(5) q.Enqueue(10) q.Enqueue(20) q.Enqueue(30) q.Dequeue() q.Enqueue(40) q.Enqueue(50) q.Dequeue() q.Enqueue(60) q.Print() }
Attempts:
2 left
💡 Hint
Track the front and rear pointers carefully after each enqueue and dequeue.
✗ Incorrect
After enqueuing 10,20,30 and dequeuing once, 10 is removed. Then enqueue 40,50. Dequeue removes 20. Enqueue 60. The queue holds 30,40,50,60 in order.
🧠 Conceptual
intermediateUnderstanding Circular Queue Behavior
Why does the queue implementation use modulo (%) operation when updating front and rear indices?
Attempts:
2 left
💡 Hint
Think about what happens when the rear reaches the last index of the array.
✗ Incorrect
The modulo operation allows the queue to reuse empty spaces at the start of the array after some elements are dequeued, making it circular.
🔧 Debug
advancedIdentify the Bug in Dequeue Method
What error will occur if the Dequeue method is called on an empty queue?
DSA Go
func (q *Queue) Dequeue() (int, bool) { val := q.items[q.front] q.front = (q.front + 1) % q.capacity q.size-- return val, true }
Attempts:
2 left
💡 Hint
What happens if size is zero and you access q.items[q.front]?
✗ Incorrect
If the queue is empty, accessing q.items[q.front] causes an index out of range runtime error.
📝 Syntax
advancedCorrect the Syntax Error in Enqueue Method
Which option fixes the syntax error in this Enqueue method snippet?
DSA Go
func (q *Queue) Enqueue(val int) bool { if q.size == q.capacity { return false } q.rear = (q.rear + 1) % q.capacity q.items[q.rear] = val q.size++ return true }
Attempts:
2 left
💡 Hint
Check if all opening braces have matching closing braces.
✗ Incorrect
The function is missing a closing brace '}' at the end causing a syntax error.
🚀 Application
expertMaximum Number of Items in Circular Queue
Given a circular queue implemented with an array of capacity 7, what is the maximum number of items it can hold without resizing?
Attempts:
2 left
💡 Hint
Circular queues often reserve one slot to distinguish full vs empty states.
✗ Incorrect
To differentiate full and empty states, one slot is left unused, so max items = capacity - 1 = 6.
