Peek Top Element of Stack in DSA Java - Time & Space Complexity
We want to understand how long it takes to look at the top item of a stack.
How does the time needed change as the stack grows?
Analyze the time complexity of the following code snippet.
public class Stack {
private int[] data;
private int top = -1;
public int peek() {
if (top == -1) throw new RuntimeException("Stack is empty");
return data[top];
}
}
This code returns the top element of the stack without removing it.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing the element at the top index in the array.
- How many times: Exactly once per peek call.
Looking at the top element takes the same amount of time no matter how big the stack is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The time stays constant regardless of stack size.
Time Complexity: O(1)
This means peeking the top element takes the same short time no matter how many items are in the stack.
[X] Wrong: "Peeking takes longer if the stack is bigger because it has to check many elements."
[OK] Correct: Peeking only looks at the top element directly by index, so it does not check other elements.
Knowing that peek is a constant-time operation helps you explain stack efficiency clearly in interviews.
"What if the stack was implemented using a linked list instead of an array? How would the time complexity of peek change?"
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
