📖 Scenario: Imagine you are building a special stack for a game score tracker. You want to keep track of the scores, but also quickly know the lowest score at any time.
🎯 Goal: Build a stack that can push scores, pop scores, and always tell the minimum score in the stack instantly.
📋 What You'll Learn
Create a stack data structure using two slices: one for all scores and one for minimum scores.
Add a variable to track the current minimum score.
Implement push and pop operations that update the minimum score stack correctly.
Implement a function to get the current minimum score.
Print the minimum score after some operations.
💡 Why This Matters
🌍 Real World
Min stacks are useful in scenarios like tracking minimum prices, scores, or temperatures efficiently in real-time.
💼 Career
Understanding min stack design helps in coding interviews and building efficient data structures for performance-critical applications.
Progress0 / 4 steps
1
Create the initial stack structure
Create two slices called stack and minStack to hold integers. Initialize both as empty slices.
DSA Go
Hint
Use varName := []int{} to create empty slices in Go.
2
Add a variable to track the current minimum
Add a variable called currentMin of type int and set it to a very large number like int(^uint(0) >> 1) to represent infinity.
DSA Go
Hint
This expression gives the maximum int value in Go, useful as a starting minimum.
3
Implement push and pop logic with minimum tracking
Write a function called push that takes an integer val, appends it to stack, and updates minStack and currentMin if val is smaller or equal to currentMin. Also write a function called pop that removes the last element from stack and updates minStack and currentMin accordingly.
DSA Go
Hint
Remember to update currentMin when pushing or popping values.
4
Print the minimum value after operations
Use the push function to add 5, 3, 7, and 2 to the stack. Then use the pop function once. Finally, print the current minimum value stored in currentMin using fmt.Println(currentMin).
DSA Go
Hint
After popping the last pushed value (2), the minimum should be 3.
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 Min Stack functionality
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 A
Quick Check:
Min Stack = Fast minimum retrieval [OK]
Hint: Min Stack always tracks minimum for O(1) retrieval [OK]
Common Mistakes:
Thinking it sorts elements
Confusing with queue behavior
Assuming it reverses elements
2. Which of the following Go code snippets correctly declares a Min Stack struct with two slices for values and minimums?
easy
A. type MinStack struct { values map[int]int; mins []int }
B. type MinStack struct { values []int; min int }
C. type MinStack struct { values []int; mins []int }
D. type MinStack struct { values []int; mins int }
Solution
Step 1: Check struct fields for Min Stack
Min Stack needs slices to store all values and track minimums at each push.
Step 2: Validate field types
Both values and mins should be slices of int for stack behavior.
Final Answer:
type MinStack struct { values []int; mins []int } -> Option C
Quick Check:
Two int slices = correct Min Stack struct [OK]
Hint: Min Stack uses two int slices for values and mins [OK]
Common Mistakes:
Using map instead of slice for values
Using int instead of slice for mins
Mixing types between fields
3. Given the following Go code for a Min Stack, what will be printed after the operations?
ms := MinStack{}
ms.Push(3)
ms.Push(5)
ms.Push(2)
ms.Push(1)
ms.Pop()
fmt.Println(ms.GetMin())
Values slice is shortened by one element correctly.
Step 2: Check mins slice operation
Mins slice is sliced to full length, so it is not shortened, causing mismatch.
Final Answer:
The mins slice is not shortened on pop -> Option D
Quick Check:
mins slice must shrink with values [OK]
Hint: Pop must shrink both values and mins slices equally [OK]
Common Mistakes:
Forgetting to shrink mins slice
Assuming Pop returns value
Clearing slices fully on pop
5. You want to design a Min Stack that supports Push, Pop, Top, and GetMin all in O(1) time. Which approach below correctly handles the minimum tracking when pushing a new element x?
hard
A. Push x to values; push x only if x < current min to mins
B. Push x to values; push min(x, current min) to mins
C. Push x to values; push current min only if x > current min to mins
D. Push x to values; do not update mins slice
Solution
Step 1: Understand min tracking logic
Each push must record the minimum so far to allow O(1) GetMin.
Step 2: Compare new element with current min
Push the smaller of x and current min onto mins slice to keep track.
Final Answer:
Push x to values; push min(x, current min) to mins -> Option B
Quick Check:
Always push min(x, current min) to mins [OK]
Hint: Always push min of new and current min to mins stack [OK]
Common Mistakes:
Pushing x only if smaller causes missing mins
Not updating mins on every push
Ignoring mins slice updates
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.