Stack Concept and LIFO Principle in DSA Typescript - Time & Space Complexity
We want to understand how fast stack operations run as the stack grows.
How does adding or removing items change with more items?
Analyze the time complexity of the following code snippet.
class Stack {
private items: number[] = [];
push(item: number): void {
this.items.push(item);
}
pop(): number | undefined {
return this.items.pop();
}
}
This code adds and removes items from the stack following last-in, first-out order.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding or removing one item at the end of the array.
- How many times: Each push or pop does this once, no loops involved.
Each push or pop works the same no matter how many items are in the stack.
| 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: The time stays the same regardless of stack size.
Time Complexity: O(1)
This means adding or removing an item takes the same short time no matter how big the stack is.
[X] Wrong: "Pushing or popping takes longer as the stack grows because it has more items."
[OK] Correct: The stack only adds or removes from the end, so it does not need to look at all items.
Knowing stack operations run in constant time shows you understand efficient data handling, a key skill in coding interviews.
"What if the stack was implemented using a linked list instead of an array? 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 the first to be removed correctly describes this behavior as "Last In, First Out".Final Answer:
Last In, First Out - the last element added is the first to be removed -> Option AQuick Check:
LIFO = Last In, First Out [OK]
- Confusing LIFO with FIFO
- Thinking first added is removed first
- Mixing up the order of insertion and removal
stack?Solution
Step 1: Recall array method to add element at end
In TypeScript, the method to add an element at the end of an array ispush().Step 2: Verify options
Only stack.push(5); usespush(5), which is correct syntax to add 5 to the stack.Final Answer:
stack.push(5); -> Option AQuick Check:
Use push() to add element to stack [OK]
- Using non-existent methods like add or insert
- Confusing push with append (not in JS/TS)
- Syntax errors like missing parentheses
const stack: number[] = []; stack.push(10); stack.push(20); stack.push(30); const popped = stack.pop(); console.log(stack);
Solution
Step 1: Trace push operations
Elements 10, 20, and 30 are pushed in order, so stack becomes [10, 20, 30].Step 2: Trace pop operation
Pop removes the last element (30), so stack becomes [10, 20].Final Answer:
[10, 20] -> Option CQuick Check:
Pop removes last item, stack left with [10, 20] [OK]
- Thinking pop removes first element
- Confusing push and pop effects
- Expecting stack unchanged after pop
const stack: number[] = []; stack.push(1); stack.push(2); const removed = stack.pop; console.log(removed);
Solution
Step 1: Check pop usage
The code assignsstack.popwithout parentheses, so it assigns the function itself, not the popped value.Step 2: Correct usage
To pop and get the value, it should bestack.pop()with parentheses to call the method.Final Answer:
pop method is not called with parentheses -> Option DQuick Check:
Call pop() with () to execute [OK]
- Forgetting parentheses on pop()
- Confusing method reference with method call
- Assuming pop returns function instead of value
[1, 2, 3] after all operations?Choose the correct sequence:
A) push(1), push(2), pop(), push(3), push(2)
B) push(1), pop(), push(2), push(3), push(1)
C) push(3), push(2), push(1), pop(), push(1)
D) push(1), push(2), push(3), pop(), push(3)
Solution
Step 1: Trace push(1), push(2), push(3), pop(), push(3) operations
Start with empty stack []:
push(1) -> [1]
push(2) -> [1, 2]
push(3) -> [1, 2, 3]
pop() removes 3 -> [1, 2]
push(3) -> [1, 2, 3]Step 2: Verify final stack
After these operations, stack is [1, 2, 3], matching the target.Final Answer:
push(1), push(2), push(3), pop(), push(3) -> Option BQuick Check:
Pop removes last, push adds correct element [OK]
- Not tracking pop effect correctly
- Assuming pop removes first element
- Mixing order of pushes and pops
Ready to prove it?
Pass the test and this pattern moves unknown
