Challenge - 5 Problems
Enqueue Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Enqueue in Circular Queue
What is the printed state of the circular queue after the following enqueue operations?
DSA Go
package main import "fmt" type CircularQueue struct { items []int front int rear int size int } func NewCircularQueue(capacity int) *CircularQueue { return &CircularQueue{ items: make([]int, capacity), front: 0, rear: -1, size: 0, } } func (q *CircularQueue) Enqueue(val int) bool { if q.size == len(q.items) { return false } q.rear = (q.rear + 1) % len(q.items) q.items[q.rear] = val q.size++ return true } func (q *CircularQueue) String() string { result := "" for i := 0; i < q.size; i++ { index := (q.front + i) % len(q.items) result += fmt.Sprintf("%d -> ", q.items[index]) } return result + "null" } func main() { q := NewCircularQueue(4) q.Enqueue(10) q.Enqueue(20) q.Enqueue(30) q.Enqueue(40) fmt.Println(q.String()) }
Attempts:
2 left
💡 Hint
The queue has capacity 4 and can hold exactly 4 elements.
✗ Incorrect
The queue has capacity 4 and all four enqueue operations succeed. The printed state is "10 -> 20 -> 30 -> 40 -> null".
❓ Predict Output
intermediateResult of Enqueue on Linked List Queue
What is the printed state of the queue after these enqueue operations on a linked list implementation?
DSA Go
package main import "fmt" type Node struct { value int next *Node } type Queue struct { front *Node rear *Node } func (q *Queue) Enqueue(val int) { node := &Node{value: val} if q.rear == nil { q.front = node q.rear = node return } q.rear.next = node q.rear = node } func (q *Queue) String() string { result := "" current := q.front for current != nil { result += fmt.Sprintf("%d -> ", current.value) current = current.next } return result + "null" } func main() { q := &Queue{} q.Enqueue(5) q.Enqueue(15) q.Enqueue(25) fmt.Println(q.String()) }
Attempts:
2 left
💡 Hint
Enqueue adds to the rear, so order is preserved.
✗ Incorrect
Enqueue adds nodes at the rear. The order is 5, then 15, then 25, so the printed queue is "5 -> 15 -> 25 -> null".
🔧 Debug
advancedIdentify the Bug in Enqueue Method
What error will occur when running this enqueue method on a queue implemented with a slice?
DSA Go
func Enqueue(queue []int, val int, capacity int) []int { if len(queue) >= capacity { return queue } queue = append(queue, val) return queue }
Attempts:
2 left
💡 Hint
Check the condition that limits queue size.
✗ Incorrect
The condition uses > capacity, so when length equals capacity, enqueue still appends, exceeding capacity. No runtime error occurs.
❓ Predict Output
advancedOutput After Multiple Enqueues and Wrap Around in Circular Queue
Given this circular queue code, what is the printed queue after these operations?
DSA Go
package main import "fmt" type CircularQueue struct { items []int front int rear int size int } func NewCircularQueue(capacity int) *CircularQueue { return &CircularQueue{ items: make([]int, capacity), front: 0, rear: -1, size: 0, } } func (q *CircularQueue) Enqueue(val int) bool { if q.size == len(q.items) { return false } q.rear = (q.rear + 1) % len(q.items) q.items[q.rear] = val q.size++ return true } func (q *CircularQueue) Dequeue() (int, bool) { if q.size == 0 { return 0, false } val := q.items[q.front] q.front = (q.front + 1) % len(q.items) q.size-- return val, true } func (q *CircularQueue) String() string { result := "" for i := 0; i < q.size; i++ { index := (q.front + i) % len(q.items) result += fmt.Sprintf("%d -> ", q.items[index]) } return result + "null" } func main() { q := NewCircularQueue(3) q.Enqueue(1) q.Enqueue(2) q.Enqueue(3) q.Dequeue() q.Enqueue(4) fmt.Println(q.String()) }
Attempts:
2 left
💡 Hint
Dequeue removes front, enqueue wraps rear index.
✗ Incorrect
After enqueueing 1,2,3 queue is full. Dequeue removes 1. Enqueue 4 adds at rear wrapping around. Queue is 2,3,4.
🧠 Conceptual
expertMaximum Number of Enqueue Operations Before Full in Circular Queue
Consider a circular queue of capacity N. Starting empty, how many successful enqueue operations can be performed before the queue is full?
Attempts:
2 left
💡 Hint
Circular queues often reserve one slot to distinguish full vs empty.
✗ Incorrect
In typical circular queue implementations, one slot is left empty to differentiate full from empty states, so maximum enqueues before full is N - 1.
