Peek lets you see the top without touching--saving your stack from chaos!
Why Peek Top Element of Stack in DSA Java?
Imagine you have a stack of books on your desk. You want to see the title of the book on the very top without removing it. If you try to pick up the top book every time just to check, you might mess up the order or drop some books.
Manually lifting the top book each time is slow and risky. You might accidentally remove it or disturb the stack. Similarly, without a simple way to peek, checking the top element in a stack can cause errors or extra work.
The peek operation lets you look at the top element of the stack without removing it. It's like glancing at the top book's title without touching the stack. This keeps the order safe and saves time.
if (!stack.isEmpty()) {
int top = stack.pop();
System.out.println(top);
stack.push(top);
}if (!stack.isEmpty()) {
int top = stack.peek();
System.out.println(top);
}Peek makes it easy to check the current top item instantly without changing the stack's order.
When undoing actions in a text editor, peek helps to see the last action without removing it, so you can decide whether to undo or not.
Manual checking risks changing the stack order.
Peek safely shows the top element without removal.
It saves time and prevents errors in stack operations.
Practice
peek() method do in a stack?Solution
Step 1: Understand the peek method purpose
The peek method is designed to look at the top element of the stack without changing the stack.Step 2: Compare with other stack methods
Unlike pop, peek does not remove the element; it only returns it.Final Answer:
Returns the top element without removing it -> Option DQuick Check:
peek() = top element without removal [OK]
- Confusing peek with pop
- Thinking peek adds elements
- Assuming peek checks emptiness
stack?Solution
Step 1: Recall Java Stack method names
The Java Stack class usespeek()to view the top element without removing it.Step 2: Check other options for correctness
top()andgetTop()are not valid Stack methods;pop()removes the top element.Final Answer:
stack.peek(); -> Option AQuick Check:
Correct syntax for peek is stack.peek() [OK]
- Using pop() instead of peek()
- Using non-existent methods like top()
- Missing parentheses in method call
Stack<Integer> stack = new Stack<>(); stack.push(10); stack.push(20); stack.push(30); System.out.println(stack.peek());
Solution
Step 1: Trace the push operations
Elements pushed in order: 10 (bottom), 20, then 30 (top).Step 2: Understand peek output
peek() returns the top element without removing it, which is 30.Final Answer:
30 -> Option BQuick Check:
Top element after pushes = 30 [OK]
- Confusing peek with pop output
- Assuming peek returns bottom element
- Expecting exception on non-empty stack
Stack<String> stack = new Stack<>(); System.out.println(stack.peek());
Solution
Step 1: Check stack initialization and usage
The stack is created but no elements are pushed before peek is called.Step 2: Understand peek behavior on empty stack
Calling peek() on an empty stack throws EmptyStackException at runtime.Final Answer:
Throws EmptyStackException at runtime -> Option AQuick Check:
peek() on empty stack throws exception [OK]
- Assuming peek returns null if empty
- Expecting compile-time error
- Thinking it prints empty string
[5, 10, 15] (5 at bottom, 15 at top), which sequence of operations will peek the top element without removing it and then add 20 on top?1. stack.peek(); 2. stack.push(20); 3. stack.pop(); 4. stack.peek();
Select the correct order of operations to achieve this.
Solution
Step 1: Peek top element without removing
Operation 1 (stack.peek()) returns 15 without removing it.Step 2: Add 20 on top
Operation 2 (stack.push(20)) adds 20 on top, so stack becomes [5, 10, 15, 20].Final Answer:
1, 2 -> Option CQuick Check:
Peek then push = 1, 2 [OK]
- Popping before peek removes top
- Pushing before peek changes top
- Incorrect operation order
Ready to prove it?
Pass the test and this pattern moves unknown
