📖 Scenario: Imagine you are building a simple calculator app that needs to keep track of numbers pushed onto a stack. You want to quickly find the smallest number in the stack at any time without searching through all numbers.
🎯 Goal: Build a MinStack data structure in Rust that supports pushing numbers, popping the top number, and retrieving the current minimum number efficiently.
📋 What You'll Learn
Create a struct called MinStack with two vectors: stack and min_stack.
Add a method push(&mut self, val: i32) that adds val to stack and updates min_stack with the current minimum.
Add a method pop(&mut self) -> Option that removes and returns the top element from stack and updates min_stack accordingly.
Add a method get_min(&self) -> Option that returns the current minimum number from min_stack.
Print the minimum after some push and pop operations.
💡 Why This Matters
🌍 Real World
MinStack is useful in calculators, undo features, and algorithms that need quick access to minimum values in a changing dataset.
💼 Career
Understanding stack design and optimization is important for software engineers working on performance-critical applications and data structure implementations.
Progress0 / 4 steps
1
Create the MinStack struct with two vectors
Create a struct called MinStack with two fields: stack and min_stack, both of type Vec. Also implement a new() method that returns an empty MinStack.
DSA Rust
Hint
Use Vec::new() to create empty vectors for both stack and min_stack.
2
Add the push method to add elements and track minimum
Add a method push(&mut self, val: i32) to MinStack. It should push val onto stack. Then push val onto min_stack if min_stack is empty or val is less than or equal to the current minimum (last element of min_stack). Otherwise, push the current minimum again to min_stack.
DSA Rust
Hint
Remember to push the current minimum again if the new value is not smaller.
3
Add pop and get_min methods
Add a method pop(&mut self) -> Option that removes and returns the top element from stack and also pops from min_stack. Add a method get_min(&self) -> Option that returns the last element of min_stack without removing it.
DSA Rust
Hint
Pop from both vectors in pop. Use last().copied() to get the minimum without removing it.
4
Test MinStack by pushing, popping, and printing minimum
Create a MinStack instance called min_stack. Push the numbers 5, 3, 7, and 2 in that order. Print the minimum using get_min(). Then pop one element and print the minimum again.
DSA Rust
Hint
Use println!("{}", min_stack.get_min().unwrap()) to print the minimum. Remember to unwrap the Option.
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 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 Rust syntax correctly declares a struct for a Min Stack with two vectors named stack and min_stack?
easy
A. struct MinStack { stack: Vec, min_stack: Vec }
B. struct MinStack { stack: [i32], min_stack: [i32] }
C. struct MinStack { stack: Vec<i32>, min_stack: Vec<i32> }
D. struct MinStack { stack: Vec<i32>, min_stack: Vec<i64> }
Solution
Step 1: Check Rust vector syntax
Vectors in Rust are declared as Vec<Type>, so Vec<i32> is correct for 32-bit integers.
Step 2: Validate struct field types
struct MinStack { stack: Vec<i32>, min_stack: Vec<i32> } uses Vec<i32> for both fields, which is correct syntax and consistent types.