Min Stack Design in DSA Javascript - Time & Space Complexity
We want to understand how fast the Min Stack operations run as the stack grows.
How does the time to push, pop, or get the minimum change with more items?
Analyze the time complexity of the following Min Stack implementation.
class MinStack {
constructor() {
this.stack = [];
this.minStack = [];
}
push(x) {
this.stack.push(x);
if (!this.minStack.length || x <= this.minStack[this.minStack.length - 1]) {
this.minStack.push(x);
}
}
pop() {
const val = this.stack.pop();
if (val === this.minStack[this.minStack.length - 1]) {
this.minStack.pop();
}
return val;
}
getMin() {
return this.minStack[this.minStack.length - 1];
}
}
This code keeps track of the minimum value using a second stack alongside the main stack.
Look at the main operations that repeat as the stack changes.
- Primary operation: push and pop on arrays (stacks) which happen once per call.
- How many times: Each operation runs once per call, no loops inside push, pop, or getMin.
Each push or pop does a fixed number of steps regardless of stack size.
| Input Size (n) | Approx. Operations per push/pop/getMin |
|---|---|
| 10 | 3-4 steps |
| 100 | 3-4 steps |
| 1000 | 3-4 steps |
Pattern observation: The number of steps stays about the same no matter how big the stack is.
Time Complexity: O(1)
This means each operation takes the same short time no matter how many items are in the stack.
[X] Wrong: "Finding the minimum must check all items, so it takes longer as the stack grows."
[OK] Correct: The min stack keeps track of minimums as items are added or removed, so getMin just looks at the top of minStack instantly.
Knowing how to keep track of minimums efficiently shows you can design smart data structures that save time.
"What if we stored the minimum value only once instead of a stack? How would that affect the time complexity?"
Practice
Min Stack data structure?Solution
Step 1: Understand the Min Stack concept
A Min Stack is designed to keep track of the minimum element at any time efficiently.Step 2: Identify the main feature
The key feature is to get the minimum element in constant time, without scanning all elements.Final Answer:
To retrieve the minimum element in constant time -> Option CQuick Check:
Min Stack = fast minimum retrieval [OK]
- Thinking it sorts elements
- Confusing with queue behavior
- Assuming it reverses elements
Solution
Step 1: Check class syntax and initialization
class MinStack { constructor() { this.stack = []; this.minStack = []; } } correctly uses ES6 class syntax with constructor initializing two arrays.Step 2: Identify incorrect options
The function constructor uses object literals {} instead of arrays. Initializing stacks to null is incorrect. Arrow functions cannot be used for class constructors.Final Answer:
class MinStack { constructor() { this.stack = []; this.minStack = []; } } -> Option AQuick Check:
Correct class and array initialization = class MinStack { constructor() { this.stack = []; this.minStack = []; } } [OK]
- Using object literals instead of arrays
- Initializing stacks as null
- Using arrow functions for classes
const minStack = new MinStack(); minStack.push(3); minStack.push(5); minStack.push(2); minStack.push(1); minStack.pop(); console.log(minStack.getMin());
Assuming
MinStack is implemented correctly with push, pop, and getMin methods.Solution
Step 1: Trace the push operations
Push 3, 5, 2, 1 in order. Stack: [3,5,2,1]. Minimum is 1.Step 2: Pop the last element and check minimum
Pop removes 1. Stack becomes [3,5,2]. Minimum now is 2.Final Answer:
2 -> Option AQuick Check:
After pop, min = 2 [OK]
- Forgetting to update min after pop
- Assuming min stays same after pop
- Confusing stack order
pop method of a Min Stack:pop() {
if (this.stack.length === 0) return null;
const popped = this.stack.pop();
if (popped === this.minStack[this.minStack.length - 1]) {
this.minStack.pop();
}
return popped;
}Solution
Step 1: Analyze pop method behavior
The method pops from stack and conditionally pops from minStack. It returns the popped value.Step 2: Check expected pop return type in Min Stack
Usually, pop in Min Stack returns void (no value). Returning popped value may cause misuse or confusion.Final Answer:
It does not check if stack is empty before popping -> Option BQuick Check:
Pop should check for empty stack before popping [OK]
- Returning popped value instead of void
- Not checking empty stack before pop
- Incorrect minStack update logic
push, pop, top, and getMin all in constant time. Which approach below is the best to achieve this?Solution
Step 1: Understand time complexity requirements
All operations must be constant time, so scanning or sorting on each operation is not allowed.Step 2: Evaluate each approach
Scanning all elements to find the minimum, traversing a linked list, or sorting on every push all take more than constant time. Using two stacks--one for all elements, one for current minimums--ensures constant time for all operations.Final Answer:
Use two stacks: one for all elements, one for current minimums -> Option DQuick Check:
Two stacks = constant time min retrieval [OK]
- Using scanning or sorting which is slow
- Confusing queue with stack behavior
- Ignoring constant time requirement
Ready to prove it?
Pass the test and this pattern moves unknown
