A Min Stack is a special stack that supports retrieving the minimum element in constant time, along with normal stack operations like push and pop.
Click to reveal answer
intermediate
How does a Min Stack keep track of the minimum element efficiently?
It uses an auxiliary stack to store the minimum values at each state, so the current minimum can be retrieved in O(1) time.
Click to reveal answer
beginner
In Min Stack design, what happens when you push a new element smaller than or equal to the current minimum?
You push the new element onto the main stack and also push it onto the min stack because it becomes the new minimum.
Click to reveal answer
beginner
What is the time complexity of push, pop, and getMin operations in a Min Stack?
All operations (push, pop, getMin) run in constant time O(1).
Click to reveal answer
intermediate
Why do we need two stacks in Min Stack design instead of one?
One stack stores all elements, and the other keeps track of minimum elements at each point, allowing quick access to the minimum without scanning all elements.
Click to reveal answer
What does the auxiliary stack in a Min Stack store?
AOnly the maximum elements
BAll elements in sorted order
CRandom elements
DMinimum elements at each state
✗ Incorrect
The auxiliary stack stores the minimum elements corresponding to each state of the main stack.
What is the time complexity to get the minimum element from a Min Stack?
AO(log n)
BO(1)
CO(n)
DO(n^2)
✗ Incorrect
The Min Stack is designed to retrieve the minimum element in constant time O(1).
When popping from a Min Stack, what must you do with the min stack?
AAlways pop from min stack
BNever pop from min stack
CPop from min stack only if top equals popped element
DPush a new element to min stack
✗ Incorrect
You pop from the min stack only if the top of the min stack equals the popped element from the main stack to keep both stacks synchronized.
If the new element pushed is greater than the current minimum, what happens to the min stack?
APush the current minimum again to min stack
BPush the new element to min stack
CDo not push anything to min stack
DClear the min stack
✗ Incorrect
The current minimum is pushed again to keep the min stack aligned with the main stack size.
Which data structure is best suited to implement a Min Stack?
AStack
BLinked List
CHeap
DQueue
✗ Incorrect
A stack is the best data structure to implement Min Stack because it supports push and pop operations efficiently.
Explain how a Min Stack maintains the minimum element during push and pop operations.
Think about how the min stack mirrors the main stack to track minimums.
You got /4 concepts.
Describe the advantages of using a Min Stack over a normal stack when minimum retrieval is needed.
Consider time complexity and operation efficiency.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a Min Stack data structure?
easy
A. To retrieve the minimum element in constant time
B. To sort elements in ascending order
C. To store elements in a queue fashion
D. To reverse the order of elements
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 A
Quick Check:
Min Stack = fast minimum retrieval [OK]
Hint: Min Stack always tracks minimum for O(1) retrieval [OK]
Common Mistakes:
Confusing Min Stack with sorting
Thinking it behaves like a queue
Assuming it reverses elements
2. Which of the following is the correct C# method signature to push an integer val onto a Min Stack?
easy
A. public void Push(string val)
B. public int Push(int val)
C. void Push()
D. public void Push(int val)
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 D
Quick Check:
Push method = void + int parameter [OK]
Hint: Push returns void and takes int parameter [OK]
Common Mistakes:
Using wrong return type like int
Missing parameter in method
Using wrong parameter type like string
3. Given the following operations on a Min Stack:
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);