Peek Front Element of Queue in DSA Java - Time & Space Complexity
We want to understand how long it takes to look at the first item in a queue.
Specifically, how does the time to peek change as the queue grows?
Analyze the time complexity of the following code snippet.
// Peek the front element of a queue
public class Queue {
private int[] data;
private int front;
private int size;
public int peek() {
if (size == 0) throw new RuntimeException("Queue is empty");
return data[front];
}
}
This code returns the front element of the queue without removing it.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing the element at the front index in the 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 the front element takes the same short time no matter how many items are in the queue.
[X] Wrong: "Peeking the front element takes longer if the queue is bigger because it has to check many items."
[OK] Correct: The peek operation directly accesses the front index without scanning the queue, so size does not affect time.
Knowing that peek is a constant time operation helps you explain efficient queue usage clearly and confidently.
"What if the queue was implemented using a linked list instead of an array? How would the time complexity of peek change?"
Practice
peek() method do in a Java Queue?Solution
Step 1: Understand the purpose of peek()
Thepeek()method is designed to look at the front element of the queue without changing the queue.Step 2: Compare with other queue methods
Unlikepoll()which removes the front element,peek()only returns it without removal.Final Answer:
Returns the front element without removing it -> Option AQuick Check:
peek() returns front element without removal [OK]
- Confusing peek() with poll() which removes element
- Thinking peek() adds elements
- Assuming peek() checks if queue is empty
q?Solution
Step 1: Recall Java Queue methods
The standard method to look at the front element without removing it ispeek().Step 2: Check other options
front()andpop()are not methods of Java Queue interface;getFirst()belongs to Deque, not Queue.Final Answer:
q.peek(); -> Option CQuick Check:
Use peek() to view front element [OK]
- Using pop() which is not in Queue interface
- Using front() which doesn't exist in Java Queue
- Confusing getFirst() with peek()
import java.util.*;
public class Main {
public static void main(String[] args) {
Queue<Integer> q = new LinkedList<>();
q.add(10);
q.add(20);
q.add(30);
System.out.println(q.peek());
q.poll();
System.out.println(q.peek());
}
}Solution
Step 1: Add elements to queue
Elements 10, 20, 30 are added in order. Queue is now [10, 20, 30].Step 2: Peek and poll operations
First peek() returns 10 (front). Then poll() removes 10. Next peek() returns 20 (new front).Final Answer:
10\n20 -> Option DQuick Check:
peek() before poll = 10, after poll = 20 [OK]
- Assuming peek() removes element
- Confusing poll() with peek()
- Expecting peek() to return last element
import java.util.*;
public class Main {
public static void main(String[] args) {
Queue<Integer> q = new LinkedList<>();
q.add(5);
q.add(15);
int front = q.peek();
System.out.println(front);
}
}Solution
Step 1: Check peek() return type
peek() returns an Integer object, which Java auto-unboxes to int automatically.Step 2: Verify code correctness
Code compiles and runs fine, printing 5 as expected.Final Answer:
No error, code runs fine and prints 5 -> Option BQuick Check:
Auto-unboxing allows Integer to int assignment [OK]
- Thinking explicit cast needed from Integer to int
- Assuming peek() returns primitive int
- Believing Queue lacks peek() method
q containing integers, write a Java method peekOrDefault that returns the front element if the queue is not empty, or returns -1 if it is empty. Which of the following implementations is correct?Solution
Step 1: Check for empty queue safely
Useq.isEmpty()to check if queue has elements before peeking.Step 2: Return front or default
If empty, return -1; else returnq.peek()which does not remove element.Step 3: Analyze other options
public int peekOrDefault(Queue<Integer> q) { return q.peek() == null ? -1 : q.poll(); }:public int peekOrDefault(Queue<Integer> q) { return q.peek() == null ? -1 : q.poll(); }peeks to check but polls to return, removing the front element—wrong. public int peekOrDefault(Queue<Integer> q) { return q.poll() == null ? -1 : q.peek(); }:public int peekOrDefault(Queue<Integer> q) { return q.poll() == null ? -1 : q.peek(); }polls first; if not empty, removes front then peeks new front—wrong. public int peekOrDefault(Queue<Integer> q) { if(q.peek() == null) return -1; else return q.poll(); }:public int peekOrDefault(Queue<Integer> q) { if(q.peek() == null) return -1; else return q.poll(); }returns poll() in else, removing element—wrong.Final Answer:
public int peekOrDefault(Queue<Integer> q) { return q.isEmpty() ? -1 : q.peek(); } -> Option AQuick Check:
Check empty first, then peek without removal [OK]
- Using poll() which removes element
- Peeking to check null but then polling which removes
- Not handling empty queue before peeking
Ready to prove it?
Pass the test and this pattern moves unknown
