Peek Front Element of Queue in DSA Typescript - Time & Space Complexity
We want to understand how long it takes to look at the front item of a queue.
Specifically, how does the time change when the queue grows bigger?
Analyze the time complexity of the following code snippet.
class Queue {
private items: number[] = [];
peek(): number | undefined {
return this.items[0];
}
}
This code returns the front element of the queue without removing it.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing the first element of an array.
- How many times: Exactly once per peek call.
Looking at the front element takes the same 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 check many items."
[OK] Correct: Peeking only looks at the first item directly, so it does not depend on the queue size.
Knowing that peek is a quick, constant-time operation helps you explain efficient queue usage in real problems.
"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 peek operation
The peek operation looks at the front element of the queue but does not remove it.Step 2: Compare with other operations
Removing is done by dequeue, adding by enqueue, and checking empty is a separate check.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 array methods for queue
In a queue using an array, the front element is at index 0.Step 2: Identify correct syntax for peek
Accessingqueue[0]returns the front without removing it.pop()removes from end,shift()removes from front,push()adds to end.Final Answer:
const front = queue[0]; -> Option DQuick Check:
Peek = queue[0] [OK]
- Using pop() which removes last element
- Using shift() which removes front element
- Using push() which adds element
const queue: number[] = [10, 20, 30]; const front = queue[0]; console.log(front); console.log(queue);
Solution
Step 1: Peek front element from queue array
Accessingqueue[0]returns 10, the first element.Step 2: Check if queue is modified
Since we only accessedqueue[0], the array remains unchanged as [10, 20, 30].Final Answer:
10 [10, 20, 30] -> Option CQuick Check:
Peek returns 10, queue unchanged [OK]
- Assuming peek removes element
- Confusing front with last element
- Expecting undefined for valid index
class Queue {
items: number[] = [];
peek() {
return this.items.pop();
}
}Solution
Step 1: Understand pop() behavior
pop() removes and returns the last element, not the front.Step 2: Correct way to peek front
To peek front, returnthis.items[0]without removing it.Final Answer:
pop() removes last element, should use items[0] -> Option AQuick Check:
pop() ≠ peek front [OK]
- Using pop() for peek
- Not returning front element correctly
- Ignoring array indexing
value and next. How do you peek the front element safely when the queue might be empty?Solution
Step 1: Understand linked list queue front
The front element is at the head node of the linked list.Step 2: Handle empty queue case
Ifheadis null, queue is empty, so return null or undefined safely.Step 3: Return front value safely
Ifheadexists, returnhead.valuewithout removing the node.Final Answer:
Check if head is null; if not, return head.value, else return null -> Option BQuick Check:
Peek front safely = check head then return value [OK]
- Not checking for empty queue
- Returning value without null check
- Removing node instead of peeking
Ready to prove it?
Pass the test and this pattern moves unknown
