Stack Concept and LIFO Principle in DSA Javascript - Time & Space Complexity
We want to understand how fast stack operations run as the stack grows.
How does the time to add or remove items change when the stack gets bigger?
Analyze the time complexity of these stack operations.
class Stack {
constructor() {
this.items = [];
}
push(element) {
this.items.push(element);
}
pop() {
return this.items.pop();
}
}
This code adds (push) and removes (pop) items from the stack following the last-in, first-out rule.
Look at what repeats when we use the stack.
- Primary operation: Adding or removing one item at the end of the array.
- How many times: Each push or pop happens once per call, no loops inside.
Each push or pop takes the same short time 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 no matter how big the stack is.
Time Complexity: O(1)
This means adding or removing an item takes a constant, quick step regardless of stack size.
[X] Wrong: "Push or pop takes longer as the stack grows because there are more items."
[OK] Correct: Push and pop only work at the top, so they do not check or move other items, keeping time constant.
Knowing stack operations run in constant time shows you understand efficient data handling, a key skill in coding interviews.
"What if we used an array but added items at the start instead of the end? How would the time complexity change?"
Practice
Solution
Step 1: Understand the LIFO principle
LIFO means the last element you put in is the first one you take out.Step 2: Apply to stack behavior
Stacks follow LIFO, so the newest item is removed first.Final Answer:
Last In, First Out - the last element added is the first to be removed -> Option DQuick Check:
LIFO = Last In, First Out [OK]
- Confusing LIFO with FIFO
- Thinking elements are removed randomly
- Assuming removal depends on element value
stack in JavaScript?Solution
Step 1: Recall JavaScript stack operations
JavaScript arrays usepush()to add elements at the end (top of stack).Step 2: Match method to stack push
push()adds element to stack top, sostack.push(5);is correct.Final Answer:
stack.push(5); -> Option CQuick Check:
Push method adds element to stack [OK]
- Using add() or insert() which are not array methods
- Confusing enqueue() with queue operation
- Trying to use non-existent methods
const stack = []; stack.push(10); stack.push(20); const popped = stack.pop(); console.log(popped); console.log(stack);
Solution
Step 1: Trace push operations
stack.push(10) adds 10, stack.push(20) adds 20. Stack is now [10, 20].Step 2: Trace pop operation
stack.pop() removes last element 20 and returns it. Stack becomes [10].Final Answer:
20 [10] -> Option AQuick Check:
Pop returns last pushed element 20 [OK]
- Thinking pop returns first element
- Confusing push and pop order
- Expecting stack unchanged after pop
const stack = []; stack.push(1); stack.push(2); const top = stack.pop; console.log(top);
Solution
Step 1: Check pop usage
stack.pop is a function reference; missing () means no call, so top is function, not value.Step 2: Understand effect on output
console.log(top) prints function code, not popped element. Correct usage is stack.pop().Final Answer:
pop is used without parentheses, so it returns the function, not the popped value -> Option AQuick Check:
Call pop() with () to get value [OK]
- Forgetting parentheses on pop()
- Thinking push method is wrong
- Confusing array with object for stack
push(1), push(2), pop(), push(3), pop(), pop()
What is the final output of each pop operation in sequence?
Solution
Step 1: Perform push(1) and push(2)
Stack becomes [1, 2].Step 2: pop() removes last pushed element
pop() returns 2, stack is now [1].Step 3: push(3)
Stack becomes [1, 3].Step 4: pop() returns 3, stack is [1]
Step 5: pop() returns 1, stack is empty
Final Answer:
[2, 3, 1] -> Option BQuick Check:
Pop returns last pushed elements in reverse order [OK]
- Mixing order of popped elements
- Assuming pop returns undefined when stack not empty
- Confusing push and pop effects
Ready to prove it?
Pass the test and this pattern moves unknown
