Stack Implementation Using Array in DSA Javascript - Time & Space Complexity
We want to understand how fast stack operations run when using an array.
Specifically, how the time changes as the stack grows.
Analyze the time complexity of the following stack operations using an array.
class Stack {
constructor() {
this.items = [];
}
push(element) {
this.items.push(element);
}
pop() {
return this.items.pop();
}
peek() {
return this.items[this.items.length - 1];
}
}
This code shows how to add, remove, and check the top item in a stack using an array.
Look at the main actions that repeat when using 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 does a simple action regardless of stack size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 pushes = 10 operations |
| 100 | 100 pushes = 100 operations |
| 1000 | 1000 pushes = 1000 operations |
Pattern observation: Operations grow directly with the number of calls, not the stack size.
Time Complexity: O(1)
This means each push or pop takes the same small amount of time no matter how big the stack is.
[X] Wrong: "Push or pop takes longer as the stack grows because the array gets bigger."
[OK] Correct: Arrays add or remove items at the end in constant time, so time does not depend on size.
Knowing stack operations are quick helps you choose the right tool for problems needing last-in-first-out order.
"What if we used an array but added or removed items at the start instead of the end? How would the time complexity change?"
Practice
Solution
Step 1: Understand stack behavior
A stack adds and removes elements from the same end, so the last element added is the first to be removed.Step 2: Identify the correct term
This behavior is called Last In, First Out (LIFO).Final Answer:
Last In, First Out (LIFO) -> Option BQuick Check:
Stack = LIFO [OK]
- Confusing stack with queue (FIFO)
- Thinking stack allows random access
- Assuming stack keeps elements sorted
Solution
Step 1: Identify method to add element at the end
In JavaScript arrays, push() adds an element to the end of the array.Step 2: Confirm stack push operation
Stack push means adding element on top, which corresponds to array's end, so push() is correct.Final Answer:
stack.push(element); -> Option AQuick Check:
Use push() to add element to stack [OK]
- Using pop() which removes element
- Using shift() or unshift() which affect start of array
const stack = []; stack.push(10); stack.push(20); stack.pop(); stack.push(30); console.log(stack);
Solution
Step 1: Trace push and pop operations
Initially stack = [] push(10) -> [10] push(20) -> [10, 20] pop() removes last element 20 -> [10] push(30) -> [10, 30]Step 2: Check final stack state
After all operations, stack contains [10, 30].Final Answer:
[10, 30] -> Option DQuick Check:
Push and pop update stack correctly [OK]
- Assuming pop removes first element
- Confusing order of elements after pop
class Stack {
constructor() {
this.items = [];
}
push(element) {
this.items.unshift(element);
}
pop() {
return this.items.pop();
}
}Solution
Step 1: Analyze push method
push uses unshift which adds element at start of array.Step 2: Analyze pop method
pop removes element from end of array. This causes mismatch because push adds at start but pop removes from end.Step 3: Fix mismatch
To maintain stack behavior, push should add at end using push(), so pop removes last added element correctly.Final Answer:
Use push() instead of unshift() in push method -> Option AQuick Check:
push() and pop() must match ends [OK]
- Mixing unshift with pop causes wrong stack order
- Thinking shift() and pop() are interchangeable
- Ignoring array end consistency
Solution
Step 1: Understand max tracking challenge
Finding max after each operation must be efficient, not scanning entire array.Step 2: Evaluate options
Sorting after each push is costly. Scanning array after pop is slow. Storing max in variable fails if max is popped.Step 3: Use two stacks approach
One stack stores elements, second stack stores current max at each push. This allows O(1) max retrieval.Final Answer:
Use two stacks: one for elements, one for max values -> Option CQuick Check:
Two stacks track max efficiently [OK]
- Sorting array each time is inefficient
- Single variable max fails when max is popped
- Scanning array after pop is slow
Ready to prove it?
Pass the test and this pattern moves unknown
