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 the following operations in a linked list queue implementation?
Operations:
Enqueue(10), Enqueue(20), Dequeue(), Enqueue(30), Dequeue()
Operations:
Enqueue(10), Enqueue(20), Dequeue(), Enqueue(30), Dequeue()
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) { newNode := &Node{value: val} if q.rear == nil { q.front = newNode q.rear = newNode return } q.rear.next = newNode q.rear = newNode } func (q *Queue) Dequeue() (int, bool) { if q.front == nil { return 0, false } val := q.front.value q.front = q.front.next if q.front == nil { q.rear = nil } return val, true } func (q *Queue) Print() { current := q.front for current != nil { fmt.Print(current.value) if current.next != nil { fmt.Print(" -> ") } current = current.next } fmt.Println(" -> null") } func main() { q := &Queue{} q.Enqueue(10) q.Enqueue(20) q.Dequeue() q.Enqueue(30) q.Dequeue() q.Print() }
Attempts:
2 left
💡 Hint
Trace each operation step-by-step and update the queue state.
✗ Incorrect
Initially, enqueue 10 and 20: queue is 10 -> 20 -> null. Dequeue removes 10: queue is 20 -> null. Enqueue 30: queue is 20 -> 30 -> null. Dequeue removes 20: queue is 30 -> null. The final print shows 30 -> null.
🧠 Conceptual
intermediateUnderstanding Queue Front and Rear Pointers
In a linked list implementation of a queue, which statement about the front and rear pointers is true after multiple enqueue and dequeue operations?
Attempts:
2 left
💡 Hint
Think about which end of the queue removes elements and which end adds elements.
✗ Incorrect
In a queue, elements are removed from the front and added at the rear. So, the front pointer points to the node to be dequeued next, and the rear pointer points to the last inserted node.
🔧 Debug
advancedIdentify the Error in Dequeue Method
What error will occur when running the following Dequeue method in a linked list queue implementation if the queue is empty?
DSA Go
func (q *Queue) Dequeue() int { val := q.front.value q.front = q.front.next if q.front == nil { q.rear = nil } return val }
Attempts:
2 left
💡 Hint
Consider what happens if q.front is nil before accessing q.front.value.
✗ Incorrect
If the queue is empty, q.front is nil. Accessing q.front.value causes a runtime panic due to nil pointer dereference.
❓ Predict Output
advancedOutput After Multiple Enqueue and Dequeue
What is the printed state of the queue after these operations?
Enqueue(5), Enqueue(15), Enqueue(25), Dequeue(), Dequeue(), Enqueue(35), Enqueue(45), Dequeue()
Enqueue(5), Enqueue(15), Enqueue(25), Dequeue(), Dequeue(), Enqueue(35), Enqueue(45), Dequeue()
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) { newNode := &Node{value: val} if q.rear == nil { q.front = newNode q.rear = newNode return } q.rear.next = newNode q.rear = newNode } func (q *Queue) Dequeue() (int, bool) { if q.front == nil { return 0, false } val := q.front.value q.front = q.front.next if q.front == nil { q.rear = nil } return val, true } func (q *Queue) Print() { current := q.front for current != nil { fmt.Print(current.value) if current.next != nil { fmt.Print(" -> ") } current = current.next } fmt.Println(" -> null") } func main() { q := &Queue{} q.Enqueue(5) q.Enqueue(15) q.Enqueue(25) q.Dequeue() q.Dequeue() q.Enqueue(35) q.Enqueue(45) q.Dequeue() q.Print() }
Attempts:
2 left
💡 Hint
Track the queue after each operation carefully.
✗ Incorrect
Start: enqueue 5,15,25 → 5->15->25->null. Dequeue removes 5 → 15->25->null. Dequeue removes 15 → 25->null. Enqueue 35,45 → 25->35->45->null. Dequeue removes 25 → 35->45->null. Final print is 35 -> 45 -> null.
🚀 Application
expertCalculate Queue Size After Operations
Given a linked list queue with these operations:
Enqueue(1), Enqueue(2), Enqueue(3), Dequeue(), Enqueue(4), Dequeue(), Dequeue(), Enqueue(5)
What is the number of elements in the queue after these operations?
Enqueue(1), Enqueue(2), Enqueue(3), Dequeue(), Enqueue(4), Dequeue(), Dequeue(), Enqueue(5)
What is the number of elements in the queue after these operations?
DSA Go
package main type Node struct { value int next *Node } type Queue struct { front *Node rear *Node } func (q *Queue) Enqueue(val int) { newNode := &Node{value: val} if q.rear == nil { q.front = newNode q.rear = newNode return } q.rear.next = newNode q.rear = newNode } func (q *Queue) Dequeue() (int, bool) { if q.front == nil { return 0, false } val := q.front.value q.front = q.front.next if q.front == nil { q.rear = nil } return val, true } func (q *Queue) Size() int { count := 0 current := q.front for current != nil { count++ current = current.next } return count } func main() { q := &Queue{} q.Enqueue(1) q.Enqueue(2) q.Enqueue(3) q.Dequeue() q.Enqueue(4) q.Dequeue() q.Dequeue() q.Enqueue(5) size := q.Size() println(size) }
Attempts:
2 left
💡 Hint
Count elements after each operation carefully.
✗ Incorrect
Operations stepwise: Enqueue 1,2,3 → size 3. Dequeue removes 1 → size 2. Enqueue 4 → size 3. Dequeue removes 2 → size 2. Dequeue removes 3 → size 1. Enqueue 5 → size 2. Final size is 2.
