Challenge - 5 Problems
Queue Peek Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after peeking the front element of the queue?
Consider the following Go code that implements a queue and peeks the front element. What will be printed?
DSA Go
package main import "fmt" type Queue struct { items []int } func (q *Queue) Enqueue(val int) { q.items = append(q.items, val) } func (q *Queue) Peek() int { if len(q.items) == 0 { return -1 } return q.items[0] } func main() { q := Queue{} q.Enqueue(10) q.Enqueue(20) q.Enqueue(30) fmt.Println(q.Peek()) }
Attempts:
2 left
💡 Hint
Peek returns the first element without removing it.
✗ Incorrect
The Peek method returns the first element in the queue, which is 10 after enqueuing 10, 20, and 30.
❓ Predict Output
intermediateWhat happens when peeking an empty queue?
Given the following Go code, what will be the output when peeking an empty queue?
DSA Go
package main import "fmt" type Queue struct { items []int } func (q *Queue) Peek() int { if len(q.items) == 0 { return -1 } return q.items[0] } func main() { q := Queue{} fmt.Println(q.Peek()) }
Attempts:
2 left
💡 Hint
Peek returns -1 if the queue is empty.
✗ Incorrect
The Peek method checks if the queue is empty and returns -1 to indicate no elements.
🔧 Debug
advancedIdentify the error in the Peek method implementation
What error will occur when running this Go code that tries to peek the front element of the queue?
DSA Go
package main import "fmt" type Queue struct { items []int } func (q *Queue) Peek() int { return q.items[0] } func main() { q := Queue{} fmt.Println(q.Peek()) }
Attempts:
2 left
💡 Hint
Peek does not check if the queue is empty before accessing the first element.
✗ Incorrect
Since the queue is empty, accessing q.items[0] causes an index out of range panic at runtime.
🧠 Conceptual
advancedWhat is the time complexity of peeking the front element in a queue implemented with a slice?
Consider a queue implemented using a slice in Go. What is the time complexity of the Peek operation?
Attempts:
2 left
💡 Hint
Peek just returns the first element without iteration.
✗ Incorrect
Accessing the first element of a slice is a constant time operation, O(1).
🚀 Application
expertAfter these operations, what is the front element of the queue?
Given the following Go code, what will be printed after all operations?
DSA Go
package main import "fmt" type Queue struct { items []int } func (q *Queue) Enqueue(val int) { q.items = append(q.items, val) } func (q *Queue) Dequeue() int { if len(q.items) == 0 { return -1 } val := q.items[0] q.items = q.items[1:] return val } func (q *Queue) Peek() int { if len(q.items) == 0 { return -1 } return q.items[0] } func main() { q := Queue{} q.Enqueue(5) q.Enqueue(10) q.Enqueue(15) q.Dequeue() q.Enqueue(20) fmt.Println(q.Peek()) }
Attempts:
2 left
💡 Hint
Dequeue removes the first element, Peek shows the new front.
✗ Incorrect
After removing 5, the front is 10. Then 20 is added at the end. Peek returns 10.
