Peek Front Element of Queue in DSA Go - Time & Space Complexity
We want to understand how long it takes to look at the first item in a queue without removing it.
How does the time needed change as the queue grows bigger?
Analyze the time complexity of the following code snippet.
// Peek returns the front element of the queue without removing it
func (q *Queue) Peek() (int, bool) {
if len(q.items) == 0 {
return 0, false // queue empty
}
return q.items[0], true
}
This code checks if the queue is empty and then returns the first element if it exists.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing the first element of the queue array.
- How many times: Exactly once per peek call, no loops or recursion involved.
Looking at the front element takes the same amount of time no matter how big the queue is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The time stays constant as the queue grows.
Time Complexity: O(1)
This means peeking at the front element takes the same short time no matter how many items are in the queue.
[X] Wrong: "Peeking takes longer if the queue is bigger because it has to look through all items."
[OK] Correct: The peek only looks at the first item directly, so it does not check the rest of the queue.
Knowing that peek is a quick, constant-time operation helps you explain efficient queue usage in real programs.
"What if the queue was implemented using a linked list instead of an array? How would the time complexity of peek change?"
Practice
Peek operation do in a queue?Solution
Step 1: Understand the queue operations
A queue supports adding elements at the back and removing from the front.Step 2: Define Peek operation
Peek returns the front element without removing it, unlike dequeue which removes it.Final Answer:
Returns the front element without removing it -> Option AQuick Check:
Peek = front element without removal [OK]
- Confusing peek with dequeue
- Thinking peek adds elements
- Assuming peek checks emptiness
queue?Solution
Step 1: Understand Go slice indexing
Go slices are zero-indexed, so the first element is at index 0.Step 2: Identify front element access
The front element of a queue is the first element, so queue[0] gives it.Final Answer:
front := queue[0] -> Option CQuick Check:
First element index = 0 [OK]
- Using len(queue) which is out of range
- Using last element instead of first
- Using pop() which is not a Go slice method
queue := []int{10, 20, 30}
front := queue[0]
fmt.Println(front)Solution
Step 1: Initialize queue slice
The queue slice contains elements [10, 20, 30].Step 2: Access front element
front := queue[0] assigns 10 to front.Step 3: Print front
fmt.Println(front) prints 10.Final Answer:
10 -> Option BQuick Check:
queue[0] = 10 [OK]
- Printing queue[1] instead of queue[0]
- Expecting last element output
- Thinking it causes runtime error
var queue []int front := queue[0] fmt.Println(front)
Solution
Step 1: Check queue initialization
queue is declared but not initialized, so length is 0.Step 2: Accessing queue[0]
Accessing queue[0] causes runtime panic because slice is empty.Final Answer:
Runtime panic: index out of range -> Option DQuick Check:
Empty slice access causes panic [OK]
- Assuming default zero value is returned
- Expecting compile-time error
- Thinking it prints nil
queue := []int{5, 10, 15}, write the correct Go code snippet to safely peek the front element and handle the empty queue case by printing "Empty" if no elements exist.Solution
Step 1: Check queue length before access
Use len(queue) > 0 to ensure queue has elements.Step 2: Print front or "Empty"
If queue has elements, print queue[0]; else print "Empty".Final Answer:
if len(queue) > 0 { fmt.Println(queue[0]) } else { fmt.Println("Empty") } -> Option AQuick Check:
Check length before access to avoid panic [OK]
- Not checking length before access
- Checking nil instead of length
- Reversing if-else logic
Ready to prove it?
Pass the test and this pattern moves unknown
