Stack Implementation Using Array in DSA Typescript - Time & Space Complexity
We want to understand how fast stack operations run when using an array.
How does the time to push or pop items change as the stack grows?
Analyze the time complexity of the following code snippet.
class Stack {
private items: number[] = [];
push(element: number): void {
this.items.push(element);
}
pop(): number | undefined {
return this.items.pop();
}
}
This code shows a stack using an array with push and pop methods.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding or removing one element at the end of the array.
- How many times: Each push or pop runs once per call, no loops inside.
Each push or pop does a single action regardless of stack size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 per push or pop |
| 100 | 1 per push or pop |
| 1000 | 1 per push or pop |
Pattern observation: The time stays the same no matter how many items are in the stack.
Time Complexity: O(1)
This means push and pop take the same short time no matter the stack size.
[X] Wrong: "Push or pop takes longer as the stack grows because the array is bigger."
[OK] Correct: The array adds or removes only at the end, so it does not need to move other items, keeping time constant.
Knowing that stack operations are quick and constant time helps you explain efficient data handling in interviews.
"What if we used an array but added or removed elements 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 order type
This behavior is called Last In First Out (LIFO).Final Answer:
It follows Last In First Out (LIFO) order -> Option DQuick Check:
Stack = LIFO [OK]
- Confusing stack with queue (FIFO)
- Thinking stack sorts elements
- Assuming random access like arrays
stack in TypeScript?Solution
Step 1: Recall array methods for stack push
In TypeScript, to add an element at the end of an array, use thepushmethod.Step 2: Verify other options
popremoves elements,insertandaddare not valid array methods.Final Answer:
stack.push(x); -> Option CQuick Check:
Push uses .push() method [OK]
- Using pop instead of push
- Using non-existent methods like insert or add
- Confusing push with pop
const stack: number[] = []; stack.push(10); stack.push(20); stack.pop(); stack.push(30); console.log(stack);
Solution
Step 1: Trace stack operations step-by-step
Initially stack = []. After push(10), stack = [10]. After push(20), stack = [10, 20]. After pop(), top element 20 is removed, stack = [10]. After push(30), stack = [10, 30].Step 2: Determine final stack state
The console.log prints the current stack array: [10, 30].Final Answer:
[10, 30] -> Option BQuick Check:
Push and pop update stack correctly [OK]
- Forgetting pop removes last element
- Assuming pop removes first element
- Confusing order of push and pop
class Stack {
private items: number[] = [];
pop(): number {
return this.items.pop();
}
}Solution
Step 1: Analyze pop method behavior
Thepop()method of array returnsnumber | undefinedif empty. Returning directly without check can cause undefined return.Step 2: Identify fix for empty stack
To avoid returning undefined, pop() should check ifitemsis empty before popping and handle that case.Final Answer:
pop() should check if stack is empty before popping -> Option AQuick Check:
Check empty before pop to avoid undefined [OK]
- Ignoring empty stack check
- Changing return type incorrectly
- Making items public unnecessarily
reverseStack that reverses the elements in the stack without using any extra stack or array? Which approach is correct?Solution
Step 1: Understand constraints
No extra stack or array allowed, so iterative swapping or extra storage is not possible.Step 2: Use recursion to reverse stack
Recursion can pop all elements, then insert each element at the bottom of the stack recursively, effectively reversing it.Step 3: Evaluate other options
Use a for loop to swap elements from start and end indices requires direct index access which is not typical for stack abstraction. Pop all elements into a new array, then push back in reverse order uses extra array. Use a queue to dequeue and enqueue elements to reverse order uses queue, which is extra data structure.Final Answer:
Use recursion to pop all elements, then insert them at bottom recursively -> Option AQuick Check:
Recursion reverses stack without extra storage [OK]
- Using extra arrays or stacks against constraints
- Trying to swap elements directly in stack
- Using other data structures like queue
Ready to prove it?
Pass the test and this pattern moves unknown
