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.
Specifically, how the time changes as the stack grows.
Analyze the time complexity of the following code snippet.
// Remove top item from stack if not empty
int pop(Stack* s) {
if (s->top == -1) {
return -1; // stack empty
}
int item = s->arr[s->top];
s->top--;
return item;
}
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: Exactly once per pop call.
Each pop removes one item with a fixed number of steps.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The time stays the same no matter how big the stack is.
Time Complexity: O(1)
This means popping an item takes the same short time no matter how many items are in the stack.
[X] Wrong: "Pop takes longer if the stack is bigger because it has to look through all items."
[OK] Correct: Pop only removes the top item directly without checking others, so size does not affect time.
Knowing that pop is very fast helps you understand stack efficiency and shows you can analyze simple but important operations clearly.
"What if the stack was implemented using a linked list instead of an array? How would the time complexity of pop 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 at the top.Step 2: Define pop operation
The pop operation removes and returns this top element, reducing the stack size by one.Final Answer:
Removes and returns the top element of the stack -> Option CQuick Check:
Pop = remove top element [OK]
- Thinking pop adds elements
- Confusing pop with peek
- Assuming pop removes bottom element
stack in C?Solution
Step 1: Understand C function call for pop
In C, stack is usually a struct or array, so pop function requires address to modify it.Step 2: Identify correct syntax
Passing&stack(address) to pop function is correct to modify stack state.Final Answer:
pop(&stack); -> Option AQuick Check:
Use & to pass stack address in C [OK]
- Using dot notation like in C++ or Java
- Calling pop without & causing no stack change
- Missing parentheses in function call
typedef struct {
int arr[3];
int top;
} Stack;
void pop(Stack *s) {
if (s->top == -1) return;
s->top--;
}
int main() {
Stack s = {{10, 20, 30}, 2};
pop(&s);
printf("%d", s.arr[s.top]);
return 0;
}Solution
Step 1: Initial stack state
Stack has elements [10, 20, 30] with top = 2 (pointing to 30).Step 2: Perform pop operation
Pop decreases top from 2 to 1, so top now points to 20.Step 3: Print element at new top
Printing s.arr[s.top] prints s.arr[1] which is 20.Final Answer:
20 -> Option AQuick Check:
Pop reduces top, new top element = 20 [OK]
- Printing old top element after pop
- Confusing array index with top value
- Ignoring top decrement in pop
void pop(Stack *s) {
if (s->top == 0) {
printf("Stack Underflow\n");
return;
}
s->top--;
}Solution
Step 1: Understand empty stack condition
Stack is empty when top is -1, not 0, because top starts at -1.Step 2: Fix condition to detect underflow
Change condition tos->top == -1to correctly detect empty stack before popping.Final Answer:
Change condition to s->top == -1 to check empty stack -> Option BQuick Check:
Empty stack top = -1, check before pop [OK]
- Checking top == 0 instead of -1
- Incrementing top on pop
- Ignoring underflow check
top initially -1, after pushing 3 elements and then popping 4 times, what will be the value of top?Solution
Step 1: Calculate top after pushing 3 elements
Starting top = -1, pushing 3 times increments top to 2 (0,1,2).Step 2: Calculate top after popping 4 times
Each pop decrements top by 1: 2 -> 1 -> 0 -> -1. The 4th pop tries to pop empty stack, top stays at -1.Final Answer:
-1 -> Option DQuick Check:
Pop beyond empty stack keeps top at -1 [OK]
- Assuming top can go below -1
- Counting pushes and pops incorrectly
- Ignoring underflow protection
Ready to prove it?
Pass the test and this pattern moves unknown
