📖 Scenario: You are building a special stack for a game score tracker. This stack not only stores scores but also quickly tells the smallest score so far. This helps players see their lowest score instantly.
🎯 Goal: Create a MinStack class that supports push, pop, top, and getMin operations efficiently.
📋 What You'll Learn
Create a class called MinStack with private members for stack storage.
Implement push(int val) to add a value to the stack.
Implement pop() to remove the top value from the stack.
Implement top() to return the top value without removing it.
Implement getMin() to return the smallest value in the stack at any time.
Use an auxiliary stack to keep track of minimum values.
💡 Why This Matters
🌍 Real World
Min stacks are useful in scenarios where you need quick access to the smallest element, such as in game score tracking, stock price monitoring, or undo operations.
💼 Career
Understanding min stack design helps in interviews and real-world coding tasks where efficient data retrieval is critical.
Progress0 / 4 steps
1
Create the MinStack class with two stacks
Create a class called MinStack with two private std::stack<int> members named stackData and stackMin. Also add a public constructor MinStack().
DSA C++
Hint
Use std::stack<int> for both stacks and define them as private members.
2
Implement the push method
Add a public method void push(int val) to the MinStack class. It should push val onto stackData. Also, push val onto stackMin if stackMin is empty or val is less than or equal to the top of stackMin. Otherwise, push the current top of stackMin again.
DSA C++
Hint
Push val to stackData. For stackMin, push val if empty or smaller, else push the current minimum again.
3
Implement pop and top methods
Add public methods void pop() and int top() to the MinStack class. pop() should remove the top elements from both stackData and stackMin. top() should return the top element of stackData without removing it.
DSA C++
Hint
Pop from both stacks in pop(). Return top of stackData in top().
4
Implement getMin and test the MinStack
Add a public method int getMin() to the MinStack class that returns the top element of stackMin. Then, in main(), create a MinStack object, push the values 5, 3, 7, 3, pop once, and print the results of top() and getMin() separated by a space.
DSA C++
Hint
Return the top of stackMin in getMin(). In main(), push the values, pop once, then print top() and getMin().
Practice
(1/5)
1. What is the main purpose of a Min Stack data structure?
easy
A. To store elements in a queue fashion
B. To retrieve the minimum element in constant time
C. To sort elements in ascending order
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 key 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 B
Quick Check:
Min Stack purpose = retrieve minimum element fast [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 way to declare a stack of integers in C++?
easy
A. stack<int> s[];
B. stack s<int>;
C. stack<int> s;
D. stack<int> s()
Solution
Step 1: Recall C++ stack declaration syntax
The correct syntax uses template with angle brackets and variable name after.
A. It does not check if stacks are empty before popping
B. It pops from minStack before popping from s, causing mismatch
C. It compares values incorrectly using == instead of <=
D. It should pop from s twice when values match
Solution
Step 1: Analyze pop operation safety
The code pops from stacks without checking if they are empty, risking runtime errors.
Step 2: Identify missing safety checks
Before calling top() or pop(), stacks should be checked for emptiness.
Final Answer:
It does not check if stacks are empty before popping -> Option A
Quick Check:
Pop safety requires empty checks [OK]
Hint: Always check stack empty before pop or top [OK]
Common Mistakes:
Ignoring empty stack checks
Misusing comparison operators
Popping stacks in wrong order
5. You want to design a Min Stack that supports push, pop, top, and getMin all in O(1) time. Which approach below correctly achieves this with minimal extra space?
hard
A. Use one stack to store values and a second stack to store minimums only when a new minimum is pushed
B. Use one stack and sort it after each push to find minimum
C. Use two stacks: one for values and one for all values pushed, then find min by scanning
D. Use a queue to store values and a stack to store minimums
Solution
Step 1: Understand O(1) min retrieval requirement
To get minimum in constant time, we must track minimums during push/pop without scanning.
Step 2: Evaluate approaches
Use one stack to store values and a second stack to store minimums only when a new minimum is pushed uses a second stack to store minimums only when a new minimum appears, saving space and ensuring O(1) operations.
Final Answer:
Use one stack to store values and a second stack to store minimums only when a new minimum is pushed -> Option A
Quick Check:
Min Stack with minimal space = two stacks, min pushed selectively [OK]
Hint: Push min only when new min found to save space [OK]
Common Mistakes:
Sorting stack after each push (slow)
Scanning stack to find min (not O(1))
Using queue which breaks stack order
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.