Stack Concept and LIFO Principle in DSA C - Time & Space Complexity
We want to understand how long it takes to add or remove items from a stack.
How does the time change when the stack grows bigger?
Analyze the time complexity of the following code snippet.
// Push operation: add item to stack
void push(int stack[], int *top, int value) {
stack[++(*top)] = value;
}
// Pop operation: remove item from stack
int pop(int stack[], int *top) {
return stack[(*top)--];
}
This code adds or removes one item from the top of the stack using the LIFO rule.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Single step to add or remove one item.
- How many times: Each push or pop does this once, no loops involved.
Each push or pop takes the same short time no matter how big the stack is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 operation per push or pop |
| 100 | 1 operation per push or pop |
| 1000 | 1 operation per push or pop |
Pattern observation: Time stays the same no matter how many items are in the stack.
Time Complexity: O(1)
This means adding or removing an item takes a fixed, quick step regardless of stack size.
[X] Wrong: "Pushing or popping takes longer as the stack grows because it has more items."
[OK] Correct: Each push or pop only changes the top item, so time does not grow with stack size.
Knowing that stack operations are quick and constant time helps you explain efficient data handling in many programs.
"What if we had to search for an item inside the stack before popping? How would the time complexity change?"
Practice
Solution
Step 1: Understand LIFO meaning
LIFO means the last element added to the stack is the first one to be removed.Step 2: Match with options
Last In, First Out - the last element added is removed first correctly describes this behavior as "Last In, First Out".Final Answer:
Last In, First Out - the last element added is removed first -> Option BQuick Check:
LIFO = Last In, First Out [OK]
- Confusing LIFO with FIFO
- Thinking first added is removed first
- Mixing up order of insertion and removal
Solution
Step 1: Identify correct function call syntax in C
In C, stack operations are usually functions like push(stack, element).Step 2: Check options
push(s, x); uses push(s, x); which is the correct syntax for pushing x onto stack s.Final Answer:
push(s, x); -> Option DQuick Check:
Function call push(stack, element) = push(s, x) [OK]
- Using object.method() syntax (not C style)
- Swapping parameter order
- Assigning instead of calling function
push(s, 10); push(s, 20); pop(s); push(s, 30); pop(s);
What is the value returned by the last pop operation?
Solution
Step 1: Trace stack after each operation
After push 10: stack = [10]
After push 20: stack = [10, 20]
pop(): removes 20, stack = [10]
push 30: stack = [10, 30]
pop(): removes 30Step 2: Identify last pop value
The last pop removes 30, so it returns 30.Final Answer:
30 -> Option AQuick Check:
Last pop returns last pushed element 30 [OK]
- Assuming FIFO order
- Forgetting previous pop removed top
- Mixing values pushed and popped
int pop(Stack *s) {
if (s->top == -1) {
return -1;
}
return s->items[s->top++];
}Solution
Step 1: Analyze pop operation logic
Pop should return the top element and then decrease the top index.Step 2: Identify error in index update
Code uses s->top++ which increases top, wrong for pop. It should be s->top-- to remove top element.Final Answer:
The top index is incremented instead of decremented -> Option CQuick Check:
Pop decreases top index, not increases [OK]
- Using post-increment instead of decrement
- Ignoring empty stack check
- Wrong return type for pop
push(s, 5); push(s, 10); pop(s); push(s, 15); push(s, 20); pop(s); pop(s);
What is the final state of the stack (from top to bottom)?
Solution
Step 1: Trace stack after each operation
push 5: stack = [5]
push 10: stack = [5, 10]
pop: removes 10, stack = [5]
push 15: stack = [5, 15]
push 20: stack = [5, 15, 20]
pop: removes 20, stack = [5, 15]
pop: removes 15, stack = [5]Step 2: Determine final stack state
After all operations, stack has only 5 left at bottom and top.Final Answer:
5 -> Option AQuick Check:
Only 5 remains after pops [OK]
- Forgetting to remove top on pop
- Mixing order of elements
- Assuming stack is empty after pops
Ready to prove it?
Pass the test and this pattern moves unknown
