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 seen so far. When pushing, it compares the new value with the current minimum and updates the auxiliary stack accordingly.
Click to reveal answer
beginner
TypeScript method signature: How do you define a push method for a Min Stack?
push(val: number): void - This method adds a value to the stack and updates the minimum stack if needed.
Click to reveal answer
intermediate
Why do we need to push the minimum value again on the min stack when the new value is equal to the current minimum?
Because multiple elements can have the same minimum value, pushing duplicates ensures the min stack correctly tracks the minimum after popping elements.
Click to reveal answer
beginner
What is the time complexity of getMin operation in a Min Stack?
The getMin operation runs in O(1) time because it returns the top element of the auxiliary min stack without any traversal.
Click to reveal answer
What does the auxiliary stack in a Min Stack store?
AMinimum values seen so far
BAll values pushed
CMaximum values seen so far
DOnly the top element
✗ Incorrect
The auxiliary stack stores the minimum values seen so far to allow constant time retrieval of the minimum.
When pushing a new value, when do you push it onto the min stack?
AOnly if it is greater than the current minimum
BAlways
CNever
DOnly if it is less than or equal to the current minimum
✗ Incorrect
You push the new value onto the min stack only if it is less than or equal to the current minimum to keep track of minimums.
What happens to the min stack when you pop the main stack?
AAlways pop from min stack
BPop from min stack if the popped value equals the min stack's top
CNever pop from min stack
DClear the min stack
✗ Incorrect
You pop from the min stack only if the popped value from the main stack equals the current minimum.
What is the time complexity of push, pop, and getMin in a Min Stack?
AO(n) for all
BO(n) for push, O(1) for pop and getMin
CO(1) for all
DO(1) for push and pop, O(n) for getMin
✗ Incorrect
All operations push, pop, and getMin run in constant time O(1) in a Min Stack.
Why is it important to handle duplicate minimum values in a Min Stack?
ATo correctly update minimum after popping duplicates
BTo save memory
CTo speed up push operation
DIt is not important
✗ Incorrect
Handling duplicates ensures the minimum is correctly updated after popping elements with the same minimum value.
Explain how a Min Stack works and how it keeps track of the minimum element efficiently.
Think about how to remember the smallest number at every step.
You got /5 concepts.
Describe the steps to implement push, pop, and getMin methods in a Min Stack.
Focus on how both stacks change during each operation.
You got /6 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 automatically
C. To store elements in ascending order
D. To remove duplicates from the stack
Solution
Step 1: Understand the Min Stack functionality
A Min Stack is designed to keep track of the minimum element efficiently while supporting normal stack operations.
Step 2: Identify the key feature
The key feature is retrieving the minimum element in constant time, which normal stacks cannot do efficiently.
Final Answer:
To retrieve the minimum element in constant time -> Option A
Quick Check:
Min Stack = constant time minimum retrieval [OK]
Hint: Min Stack always gives min element fast [OK]
Common Mistakes:
Thinking it sorts elements automatically
Confusing with normal stack behavior
Assuming it removes duplicates
2. Which of the following is the correct TypeScript method signature to push an element x onto a Min Stack class?
easy
A. push(x: number): number
B. push(x: string): number
C. push(x: number): void
D. push(): number
Solution
Step 1: Understand push method requirements
The push method should accept a number and return nothing (void) because it adds an element to the stack.
Step 2: Match method signature
push(x: number): void matches: it takes a number and returns void, which is correct for push operation.