Why Stack Exists and What Problems It Solves in DSA Typescript - Performance Analysis
Stacks help us manage data in a last-in, first-out way. Understanding their time complexity shows why they are fast and useful for certain problems.
We want to know how the time to add or remove items grows as the stack gets bigger.
Analyze the time complexity of the following stack operations.
class Stack {
private items: number[] = [];
push(item: number): void {
this.items.push(item);
}
pop(): number | undefined {
return this.items.pop();
}
}
This code shows how to add (push) and remove (pop) items from a stack using an array.
Look at the main actions that happen repeatedly 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 these methods.
Each push or pop takes about the same time no matter how many items are in the stack.
| Input Size (n) | Approx. Operations per push/pop |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The time stays constant even as the stack grows larger.
Time Complexity: O(1)
This means adding or removing an item from the stack takes the same short time no matter how many items are inside.
[X] Wrong: "Pushing or popping items takes longer as the stack grows because it has more data."
[OK] Correct: The stack only adds or removes from the end, so it does not need to look through all items. Each operation is quick and does not slow down with size.
Knowing that stack operations are fast and simple helps you explain why stacks are great for tasks like undo features, parsing expressions, or tracking function calls.
"What if we changed the stack to add or remove items from the start of the array instead of the end? How would the time complexity change?"
Practice
Solution
Step 1: Understand the purpose of a stack
A stack stores items in Last In First Out (LIFO) order, which is perfect for tracking function calls.Step 2: Relate stack to function calls
When a function calls another, the current function is paused and saved on the stack. When the called function finishes, the program returns to the last saved function.Final Answer:
To keep track of function calls and return in the correct order -> Option CQuick Check:
Stack = function call tracking [OK]
- Thinking stack stores data randomly
- Confusing stack with queue
- Assuming stack sorts data automatically
myStack in TypeScript?Solution
Step 1: Recall stack method names in TypeScript
Stacks usepush()to add elements on top.Step 2: Identify correct method for adding element
push()is the standard method; others likeadd(),insert(), orenqueue()are not used for stacks.Final Answer:
myStack.push(5); -> Option AQuick Check:
Push adds element on top [OK]
- Using enqueue which is for queues
- Using add or insert which are not stack methods
- Confusing push with pop
const stack: number[] = []; stack.push(10); stack.push(20); const popped = stack.pop(); stack.push(30); console.log(stack);
Solution
Step 1: Trace stack operations step-by-step
Initially stack is empty []. Push 10 -> [10]. Push 20 -> [10, 20]. Pop removes 20 -> [10]. Push 30 -> [10, 30].Step 2: Determine final stack content
After all operations, stack contains [10, 30].Final Answer:
[10, 30] -> Option DQuick Check:
Push/pop changes stack top [OK]
- Forgetting pop removes top element
- Assuming pop returns stack without removing
- Mixing order of push and pop
const stack: number[] = []; stack.push(1); stack.push(2); const top = stack.peek(); console.log(top);
Solution
Step 1: Check methods available on array used as stack
TypeScript arrays have push() and pop(), but no peek() method by default.Step 2: Identify cause of error
Calling stack.peek() will cause a runtime error because peek() is not defined.Final Answer:
Stack does not have a peek() method by default -> Option AQuick Check:
peek() missing in array stack [OK]
- Assuming peek() exists on arrays
- Thinking push() is wrong
- Confusing type declaration with method error
"hello"?Solution
Step 1: Understand stack reversal logic
Pushing characters one by one stores them in order. Popping them reverses the order because stack is LIFO.Step 2: Apply to string reversal
Push 'h', 'e', 'l', 'l', 'o' then pop all to get 'o', 'l', 'l', 'e', 'h' which is reversed string.Final Answer:
Push each character onto stack, then pop all to build reversed string -> Option BQuick Check:
Stack LIFO reverses order [OK]
- Pushing entire string at once
- Popping in same order as push
- Using stack for sorting instead of reversing
Ready to prove it?
Pass the test and this pattern moves unknown
