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 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 retrieving the minimum element from a Min Stack?
The time complexity is O(1) because the minimum element is always at the top of the auxiliary min stack.
Click to reveal answer
intermediate
Why do we need to pop from both the main stack and the min stack in a Min Stack?
Because when the top element is removed from the main stack, if it is the current minimum, it must also be removed from the min stack to update the minimum correctly.
Click to reveal answer
What data structure is typically used alongside the main stack in a Min Stack design?
AQueue to track order
BAuxiliary stack to track minimums
CHash map to store counts
DLinked list for fast access
✗ Incorrect
An auxiliary stack is used to keep track of the minimum elements at each state.
What is the time complexity for push, pop, and getMin operations in a Min Stack?
AO(1) for all
BO(n) for getMin
CO(log n) for push
DO(n) for pop
✗ Incorrect
All operations including getMin run in constant time O(1) in a Min Stack.
When pushing a new element equal to the current minimum, what should happen in the min stack?
AClear the min stack
BDo not push onto the min stack
CPop the min stack
DPush the new element onto the min stack
✗ Incorrect
Equal minimum values are pushed onto the min stack to keep track of duplicates.
If the main stack is empty, what should getMin() return or do?
AReturn the last minimum
BReturn zero
CReturn an error or indicate empty stack
DReturn the maximum element
✗ Incorrect
If the stack is empty, getMin should handle this gracefully, usually by error or special value.
Why is it not enough to keep only one minimum value variable in Min Stack design?
ABecause minimum can change after popping elements
BBecause variables are slow
CBecause stacks cannot store variables
DBecause minimum is always the first element
✗ Incorrect
The minimum can change when elements are popped, so we need to track minimums at each state.
Explain how a Min Stack maintains the minimum element during push and pop operations.
Think about how the min stack mirrors the main stack for minimum values.
You got /4 concepts.
Describe the time complexity of all operations in a Min Stack and why it is efficient.
Focus on how the auxiliary stack helps avoid scanning.
You got /3 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 along with stack operations
B. To sort elements in ascending order automatically
C. To store only the minimum elements pushed so far
D. To reverse the order of elements pushed into the stack
Solution
Step 1: Understand stack operations
A stack supports push, pop, and top operations in LIFO order.
Step 2: Identify Min Stack feature
Min Stack extends stack by allowing retrieval of minimum element in O(1) time.
Final Answer:
To retrieve the minimum element in constant time along with stack operations -> Option A
Quick Check:
Min Stack = constant time min retrieval [OK]
Hint: Min Stack always tracks minimum for O(1) min retrieval [OK]
Common Mistakes:
Thinking Min Stack sorts elements automatically
Assuming Min Stack stores only minimum elements
Confusing Min Stack with reversing stack order
2. Which of the following is the correct Java method signature to push an integer val onto a Min Stack?
easy
A. public int push(int val)
B. void push(int val) public
C. public void push(int val)
D. push public void(int val)
Solution
Step 1: Recall Java method syntax
Java method signature starts with access modifier, return type, method name, and parameters.
Step 2: Check each option
public void push(int val) follows correct syntax: public void push(int val). Others have wrong order or return type.
Final Answer:
public void push(int val) -> Option C
Quick Check:
Java method syntax = public void push(int val) [OK]
Hint: Java methods start with access modifier then return type [OK]
Common Mistakes:
Placing 'public' after method name
Using wrong return type for void methods
Incorrect parameter placement
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();
System.out.println(min);