📖 Scenario: Imagine you are building a special stack for a game score tracker. This stack not only stores scores but also quickly tells you the lowest score so far. This helps players see their worst performance instantly.
🎯 Goal: You will build a MinStack class that supports push, pop, top, and get_min operations all in constant time.
📋 What You'll Learn
Create a class called MinStack with an __init__ method
Implement a push method that adds an integer to the stack
Implement a pop method that removes the top element
Implement a top method that returns the top element
Implement a get_min method that returns the smallest element in the stack
Use an auxiliary stack to keep track of minimum values
💡 Why This Matters
🌍 Real World
Min stacks are useful in algorithms where you need quick access to the smallest element, such as in stock price tracking or game score analysis.
💼 Career
Understanding how to design efficient data structures like MinStack is important for software engineering roles that require optimization and algorithmic problem solving.
Progress0 / 4 steps
1
Create the MinStack class with initialization
Create a class called MinStack with an __init__ method that initializes two empty lists: stack and min_stack.
DSA Python
Hint
Use two lists to store all elements and minimum elements separately.
2
Add the push method
Add a method called push that takes an integer x and appends it to self.stack. Also, append x to self.min_stack if it is empty or if x is less than or equal to the last element in self.min_stack.
DSA Python
Hint
Remember to check if min_stack is empty before comparing.
3
Add pop and top methods
Add a method called pop that removes the last element from self.stack. If the popped element is equal to the last element in self.min_stack, also remove it from self.min_stack. Add a method called top that returns the last element of self.stack.
DSA Python
Hint
Pop from min_stack only if the popped value equals its last element.
Return the last element of stack in top.
4
Add get_min method and print test output
Add a method called get_min that returns the last element of self.min_stack. Then create a MinStack object, push the values 5, 3, 7, pop once, and print the result of get_min().
DSA Python
Hint
The minimum after popping 7 should be 3.
Practice
(1/5)
1. What is the main purpose of a Min Stack data structure?
easy
A. To store only the minimum elements pushed so far
B. To sort elements in ascending order automatically
C. To retrieve the minimum element in constant time along with stack operations
D. To reverse the order of elements pushed into the stack
Solution
Step 1: Understand the Min Stack concept
A Min Stack is designed to support normal stack operations and also return the minimum element quickly.
Step 2: Identify the key feature
The key feature is retrieving the minimum element in constant time, not sorting or reversing.
Final Answer:
To retrieve the minimum element in constant time along with stack operations -> Option C
Quick Check:
Min Stack = fast minimum retrieval [OK]
Hint: Min Stack always tracks minimum for O(1) retrieval [OK]
Common Mistakes:
Thinking Min Stack sorts elements
Assuming it stores only minimum elements
Confusing with reversing stack order
2. Which of the following is the correct way to initialize a Min Stack class in Python?
easy
A. class MinStack:
def __init__(self):
stack = []
min_stack = []
B. class MinStack:
def __init__(self):
self = []
C. class MinStack:
def __init__(self):
self.stack = None
self.min_stack = None
D. class MinStack:
def __init__(self):
self.stack = []
self.min_stack = []
Solution
Step 1: Check correct attribute initialization
Attributes must be initialized with self to be instance variables.
Step 2: Verify list initialization
Both stack and min_stack should be empty lists initialized with self.
Final Answer:
class MinStack:\n def __init__(self):\n self.stack = []\n self.min_stack = [] -> Option D
Quick Check:
Use self for instance variables [OK]
Hint: Always use self to define instance variables in __init__ [OK]
Common Mistakes:
Not using self for attributes
Initializing attributes as None instead of lists
Assigning self to a list directly
3. What will be the output of the following code snippet?