Pop Operation on Stack in DSA C++ - Time & Space Complexity
We want to understand how long it takes to remove an item from a stack.
How does the time needed change as the stack grows bigger?
Analyze the time complexity of the following code snippet.
// Remove the top item from the stack
int pop() {
if (isEmpty()) {
throw std::out_of_range("Stack is empty");
}
int topItem = stackArray[topIndex];
topIndex--;
return topItem;
}
This code removes the top element from the stack and returns it.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing and removing the top element.
- How many times: This happens once per pop call, no loops or recursion.
Removing the top item always takes the same steps, no matter how big the stack is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 3 steps (check empty, access top, decrease index) |
| 100 | 3 steps (same as above) |
| 1000 | 3 steps (same as above) |
Pattern observation: The number of steps stays the same no matter how large the stack is.
Time Complexity: O(1)
This means popping an item takes a fixed amount of time regardless of stack size.
[X] Wrong: "Pop takes longer if the stack is bigger because there are more items."
[OK] Correct: Pop only removes the top item directly, so it does not depend on stack size.
Knowing that pop is a quick, 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 change?"
Practice
pop() operation do in a stack?Solution
Step 1: Understand stack behavior
A stack follows Last In First Out (LIFO) principle, so the last added element is removed first.Step 2: Define pop operation
The pop operation removes the top element from the stack, which is the last inserted item.Final Answer:
Removes the top element from the stack -> Option BQuick Check:
pop() removes top element [OK]
- Thinking pop adds elements
- Confusing pop with peek or size
- Assuming pop clears the stack
stk in C++ STL?Solution
Step 1: Recall C++ STL stack syntax
The standard stack class in C++ uses member functions like push() and pop() called on the stack object.Step 2: Identify correct pop syntax
The correct way to remove the top element is callingpop()asstk.pop();.Final Answer:
stk.pop(); -> Option AQuick Check:
Use object.pop() to remove top [OK]
- Using pop() as a free function
- Using incorrect method names like pop_element()
- Trying to remove with remove() which doesn't exist
std::stack<int> stk; stk.push(10); stk.push(20); stk.pop(); std::cout << stk.top();
Solution
Step 1: Trace push operations
First, 10 is pushed, then 20 is pushed on top. Stack top is now 20.Step 2: Apply pop and check top
pop() removes the top element (20), so top becomes 10. Printing top outputs 10.Final Answer:
10 -> Option CQuick Check:
After pop, top is 10 [OK]
- Assuming top remains 20 after pop
- Confusing pop with peek
- Expecting 0 or error output
std::stack<int> stk; stk.pop(); stk.push(5);
Solution
Step 1: Analyze stack state before pop
The stack is empty initially. Calling pop() on empty stack is invalid and causes runtime error.Step 2: Check order of operations
Pop is called before any push, so no element exists to remove, causing error.Final Answer:
Pop called on empty stack causes runtime error -> Option DQuick Check:
Pop on empty stack causes error [OK]
- Ignoring empty stack before pop
- Thinking push order fixes error
- Assuming pop on empty is safe
1, 2, 3, 4 pushed in that order (1 at bottom, 4 at top), what will be the stack content after performing two pop() operations?Solution
Step 1: Understand initial stack order
Elements pushed in order: 1 (bottom), 2, 3, 4 (top). Stack looks like 1 -> 2 -> 3 -> 4 -> null.Step 2: Apply two pop operations
First pop removes 4 (top), second pop removes 3 (new top). Remaining stack is 1 -> 2 -> null.Final Answer:
1 -> 2 -> null -> Option AQuick Check:
Two pops remove top two elements [OK]
- Confusing order of elements after pop
- Listing popped elements instead of remaining
- Reversing stack order incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
