📖 Scenario: Imagine you are building a special stack for a calculator app. This stack not only stores numbers but also quickly tells you the smallest number currently in the stack. This helps the calculator show the minimum value instantly without searching through all numbers.
🎯 Goal: Create a MinStack class that supports these operations: Push to add a number, Pop to remove the top number, Top to see the top number, and GetMin to get the smallest number in the stack at any time.
📋 What You'll Learn
Create a MinStack class with a constructor
Implement Push(int val) to add a number
Implement Pop() to remove the top number
Implement Top() to return the top number
Implement GetMin() to return the smallest number
Use two stacks internally: one for all numbers, one for tracking minimums
💡 Why This Matters
🌍 Real World
Min Stacks are useful in applications like calculators, stock price tracking, or any system needing quick access to minimum values during data processing.
💼 Career
Understanding Min Stack design helps in coding interviews and real-world software development where efficient data retrieval is critical.
Progress0 / 4 steps
1
Create the MinStack class with two stacks
Create a public class called MinStack with a constructor that initializes two private stacks: stack and minStack, both of type Stack<int>.
DSA C#
Hint
Use stack = new Stack(); and minStack = new Stack(); inside the constructor.
2
Implement the Push method
Add a public method Push that takes an int val. It should push val onto stack. Then, push val onto minStack if minStack is empty or val is less than or equal to the current top of minStack. Otherwise, push the current top of minStack again to keep track of the minimum.
DSA C#
Hint
Push val to stack. For minStack, push val if it's smaller or equal to current min, else push the current min again.
3
Implement Pop and Top methods
Add two public methods: Pop() that removes the top element from both stack and minStack, and Top() that returns the top element of stack without removing it.
DSA C#
Hint
Pop from both stacks in Pop(). Return stack.Peek() in Top().
4
Implement GetMin method and print test output
Add a public method GetMin() that returns the top element of minStack. Then, create a Main method to test your MinStack by pushing 3, 5, 2, 1, popping once, and printing the results of Top() and GetMin().
DSA C#
Hint
Return minStack.Peek() in GetMin(). In Main(), push 3, 5, 2, 1, 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 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);