What if you could see the next in line without disturbing anyone? Peek makes it easy!
Why Peek Front Element of Queue in DSA Java?
Imagine you are waiting in a line to buy movie tickets. You want to know who is next without letting anyone leave the line. You try to ask everyone to step aside and check, but it causes confusion and delays.
Manually checking the first person in line by moving everyone around is slow and can cause mistakes like losing someone's place. It wastes time and makes the line messy.
The peek operation lets you see the first person in line without removing them. It keeps the order intact and quickly tells you who is next, just like glancing at the front of the queue without disturbing anyone.
if (!queue.isEmpty()) {
int first = queue.remove();
System.out.println(first);
queue.add(first);
}if (!queue.isEmpty()) {
int first = queue.peek();
System.out.println(first);
}This lets programs quickly check the next item to process without changing the queue, enabling smooth and efficient task management.
In a printer queue, peek shows which document will print next without removing it, so you can decide to cancel or reorder without losing the queue order.
Manually checking front by removing and re-adding is slow and risky.
Peek shows the front element without changing the queue.
It helps keep order and speeds up decision-making.
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
