Peek Top Element of Stack in DSA C++ - 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 when the stack grows bigger?
Analyze the time complexity of the following code snippet.
int peekTop(const std::stack<int>& s) {
if (s.empty()) {
throw std::out_of_range("Stack is empty");
}
return s.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 top element of the stack.
- How many times: Exactly once per call.
Looking at the top element takes the same time no matter how many items are in the stack.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The time stays constant even if the stack grows larger.
Time Complexity: O(1)
This means peeking the top element takes the same short time no matter how big the stack is.
[X] Wrong: "Peeking the top element takes longer if the stack has more items."
[OK] Correct: The stack keeps track of the top item directly, so accessing it does not depend on stack size.
Knowing that peek is a constant time operation helps you explain stack efficiency clearly and confidently.
"What if the stack was implemented using a linked list without a direct pointer to the top? How would the time complexity change?"
Practice
peek() operation do in a stack?Solution
Step 1: Understand stack operations
A stack is a collection where elements are added or removed from the top only.Step 2: Define peek operation
The peek operation returns the top element without removing it, unlike pop which removes it.Final Answer:
Returns the top element without removing it -> Option AQuick Check:
peek() = top element [OK]
- Confusing peek with pop
- Thinking peek adds elements
- Assuming peek checks emptiness
stk in C++ STL?Solution
Step 1: Recall C++ STL stack methods
The standard method to access the top element istop().Step 2: Identify correct syntax
stk.top();returns the top element without removing it.Final Answer:
stk.top(); -> Option CQuick Check:
stk.top() = peek top element [OK]
- Using .peek() which is not in C++ STL
- Using .pop() which removes element
- Using non-existent .getTop() method
std::stack<int> stk; stk.push(10); stk.push(20); stk.push(30); std::cout << stk.top() << std::endl;
Solution
Step 1: Trace push operations
Elements pushed in order: 10, then 20, then 30. Stack top is last pushed element 30.Step 2: Check top() output
stk.top()returns 30 without removing it.Final Answer:
30 -> Option AQuick Check:
Last pushed = top = 30 [OK]
- Confusing top with bottom element
- Assuming top removes element
- Thinking output is 20 or 10
stk:int topElement = stk.peek();
Solution
Step 1: Check method availability in C++ STL stack
C++ STL stack does not have a method namedpeek().Step 2: Correct method to peek top element
The correct method istop(), sostk.top()should be used.Final Answer:
peek() is not a valid method in C++ STL stack -> Option BQuick Check:
Use stk.top() not stk.peek() [OK]
- Using peek() method which doesn't exist
- Confusing pop() with peek()
- Thinking topElement must be pointer
stk with elements [5, 10, 15] pushed in that order, which code snippet correctly prints the top element without removing it and then prints the stack size?Solution
Step 1: Understand peek without removal
To peek, usestk.top()which returns top element without removing it.Step 2: Check stack size after peek
Since peek does not remove,stk.size()remains 3.Final Answer:
std::cout << stk.top() << std::endl; std::cout << stk.size() << std::endl; -> Option DQuick Check:
top() no removal + size() correct [OK]
- Using pop() which removes element
- Using peek() which is invalid in C++ STL
- Printing size after pop() expecting no change
Ready to prove it?
Pass the test and this pattern moves unknown
