Min Stack Design in DSA C# - Time & Space Complexity
We want to understand how fast the Min Stack operations run as the number of elements grows.
Specifically, how the time to push, pop, or get the minimum changes with more items.
Analyze the time complexity of the following Min Stack implementation.
public class MinStack {
private Stack<int> stack = new Stack<int>();
private Stack<int> minStack = new Stack<int>();
public void Push(int val) {
stack.Push(val);
if (minStack.Count == 0 || val <= minStack.Peek()) {
minStack.Push(val);
}
}
public void Pop() {
if (stack.Peek() == minStack.Peek()) {
minStack.Pop();
}
stack.Pop();
}
public int GetMin() {
return minStack.Peek();
}
}
This code keeps track of the minimum value at each push using two stacks.
Look at the operations that happen repeatedly as we add or remove elements.
- Primary operation: Push and Pop on stacks, and Peek to check minimum.
- How many times: Each operation runs once per call, no loops inside.
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 the same no matter how big the stack gets.
Time Complexity: O(1)
This means each operation takes the same small amount of time no matter how many items are in the stack.
[X] Wrong: "Finding the minimum must check all elements, so it takes longer as stack grows."
[OK] Correct: The minStack keeps track of minimums so GetMin just looks at the top, no scanning needed.
Understanding how to keep track of minimums efficiently shows you can design smart data structures that save time.
"What if we stored minimums only when they change, not every time? How would that affect 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 efficiently.Step 2: Identify the main feature
It allows retrieving the minimum element in constant time, unlike a normal stack.Final Answer:
To retrieve the minimum element in constant time -> Option AQuick Check:
Min Stack = fast minimum retrieval [OK]
- Confusing Min Stack with sorting
- Thinking it behaves like a queue
- Assuming it reverses elements
val onto a Min Stack?Solution
Step 1: Check method return type and parameters
Push method should not return a value, so return type is void, and it takes an int parameter.Step 2: Match the correct signature
public void Push(int val) matches: public void Push(int val)Final Answer:
public void Push(int val) -> Option DQuick Check:
Push method = void + int parameter [OK]
- Using wrong return type like int
- Missing parameter in method
- Using wrong parameter type like string
MinStack minStack = new MinStack(); minStack.Push(3); minStack.Push(5); minStack.Push(2); minStack.Push(1); minStack.Pop(); int min = minStack.GetMin(); Console.WriteLine(min);
What will be printed?
Solution
Step 1: Trace the stack after each operation
Push 3 -> stack: [3], min=3 Push 5 -> stack: [3,5], min=3 Push 2 -> stack: [3,5,2], min=2 Push 1 -> stack: [3,5,2,1], min=1 Pop -> removes 1, stack: [3,5,2], min=2Step 2: GetMin after pop
Minimum is now 2 after popping 1.Final Answer:
2 -> Option BQuick Check:
Min after pop = 2 [OK]
- Assuming min stays 1 after pop
- Confusing stack top with min
- Ignoring pop effect on min
public void Pop() {
if(stack.Count > 0) {
stack.Pop();
minStack.Pop();
}}What is the main issue with this implementation?
Solution
Step 1: Understand the role of minStack
minStack should only pop when the popped element equals the current minimum.Step 2: Identify the bug
This code pops minStack every time, even if the popped element is not minimum, causing incorrect min tracking.Final Answer:
It pops from minStack even when the popped element is not the minimum -> Option CQuick Check:
Pop minStack only if popped element == min [OK]
- Always popping minStack blindly
- Not checking stack emptiness (not in this code)
- Confusing push and pop operations
Push, Pop, Top, and GetMin all in O(1) time. Which approach is best?Solution
Step 1: Analyze time complexity requirements
All operations must be O(1), so sorting or scanning is too slow.Step 2: Identify suitable data structure
Using two stacks, one for elements and one for minimums, allows constant time min retrieval.Final Answer:
Use two stacks: one for all elements, one for current minimums -> Option AQuick Check:
Two stacks = O(1) min tracking [OK]
- Sorting stack after each push (slow)
- Using queue instead of stack
- Scanning list for min on each GetMin
Ready to prove it?
Pass the test and this pattern moves unknown
