📖 Scenario: You are managing a small event ticketing system. You want to keep track of ticket prices to quickly find the cheapest ticket available and also the most expensive ticket for VIP customers.
🎯 Goal: Build two heaps: a min heap to find the cheapest ticket and a max heap to find the most expensive ticket. Learn when to use each heap type.
📋 What You'll Learn
Create a vector called tickets with exact prices: 50, 20, 70, 10, 90
Create a min heap from tickets using BinaryHeap with reversed order
Create a max heap from tickets using BinaryHeap normally
Print the smallest ticket price from the min heap
Print the largest ticket price from the max heap
💡 Why This Matters
🌍 Real World
Min heaps are used when you want to quickly find the smallest item, like the cheapest ticket. Max heaps are used when you want the largest item, like the most expensive ticket or highest priority task.
💼 Career
Understanding heaps is important for jobs involving priority queues, scheduling, and real-time data processing where quick access to min or max values is needed.
Progress0 / 4 steps
1
Create the ticket prices vector
Create a vector called tickets with these exact ticket prices: 50, 20, 70, 10, 90
DSA Rust
Hint
Use vec![...] to create the vector with the exact prices.
2
Create min heap and max heap from tickets
Add these lines below the tickets vector to create two heaps: a min heap called min_heap using BinaryHeap::from with reversed order, and a max heap called max_heap using BinaryHeap::from normally. Use std::collections::BinaryHeap and std::cmp::Reverse.
DSA Rust
Hint
Use tickets.iter().map(|&x| Reverse(x)).collect() to create a vector of reversed values for the min heap.
3
Extract smallest and largest ticket prices
Use min_heap.peek() to get the smallest ticket price and max_heap.peek() to get the largest ticket price. Store them in variables smallest and largest respectively. Remember to unwrap the Option values and unwrap the Reverse for the min heap.
DSA Rust
Hint
Use peek() to look at the top element without removing it. For min heap, unwrap Reverse by accessing .0.
4
Print the smallest and largest ticket prices
Print the smallest ticket price stored in smallest and the largest ticket price stored in largest using println!. Print the smallest price first, then the largest price on the next line.
DSA Rust
Hint
Use two println! statements to print the values on separate lines.
Practice
(1/5)
1. What is the main difference between a Min Heap and a Max Heap?
easy
A. Min Heap always keeps the smallest element at the root, Max Heap keeps the largest.
B. Min Heap stores elements in ascending order, Max Heap stores in descending order.
C. Min Heap is used only for sorting, Max Heap is used only for searching.
D. Min Heap and Max Heap have the same structure but different sizes.
Solution
Step 1: Understand heap root property
Min Heap keeps the smallest element at the root node, Max Heap keeps the largest element at the root node.
Step 2: Compare heap behavior
This difference defines how elements are ordered and accessed quickly in each heap type.
Final Answer:
Min Heap always keeps the smallest element at the root, Max Heap keeps the largest. -> Option A
Quick Check:
Root element defines heap type = A [OK]
Hint: Root element shows heap type: smallest or largest [OK]
Common Mistakes:
Confusing heap order with sorted order
Thinking heaps store all elements sorted
Mixing heap usage with sorting algorithms
2. Which Rust standard library type is used to implement a max heap by default?
easy
A. std::collections::MinHeap
B. std::collections::Heap
C. std::collections::MaxHeap
D. std::collections::BinaryHeap
Solution
Step 1: Recall Rust heap types
Rust's standard library provides BinaryHeap which is a max heap by default.
Step 2: Check other options
There are no separate MinHeap or MaxHeap types in std::collections; Heap does not exist.
Final Answer:
std::collections::BinaryHeap -> Option D
Quick Check:
Rust default heap = BinaryHeap (max) [OK]
Hint: Rust BinaryHeap is max heap by default [OK]
Common Mistakes:
Assuming MinHeap exists in std::collections
Confusing BinaryHeap with generic Heap
Thinking MaxHeap is a separate type
3. What will be the output of this Rust code snippet?
use std::collections::BinaryHeap;
fn main() {
let mut heap = BinaryHeap::new();
heap.push(10);
heap.push(5);
heap.push(20);
println!("{}", heap.peek().unwrap());
}
medium
A. 5
B. 20
C. None
D. 10
Solution
Step 1: Understand BinaryHeap behavior
BinaryHeap in Rust is a max heap, so the largest element is at the root.
Step 2: Check inserted elements
Elements 10, 5, and 20 are pushed; 20 is the largest and will be at the root.
Final Answer:
20 -> Option B
Quick Check:
Max element at root = 20 [OK]
Hint: peek() returns max element in Rust BinaryHeap [OK]
Common Mistakes:
Thinking peek() returns smallest element
Confusing heap order with insertion order
Assuming peek() returns None if heap not empty
4. Identify the error in this Rust code that tries to create a min heap using BinaryHeap:
use std::collections::BinaryHeap;
fn main() {
let mut min_heap = BinaryHeap::new();
min_heap.push(Reverse(10));
min_heap.push(Reverse(20));
println!("{}", min_heap.peek().unwrap());
}
medium
A. Missing import of Reverse from std::cmp
B. BinaryHeap cannot store Reverse types
C. peek() returns None for min heap
D. Reverse is not used for min heap in Rust
Solution
Step 1: Check usage of Reverse
The code uses Reverse wrapper but does not import it from std::cmp.
Step 2: Confirm correct import needed
Without use std::cmp::Reverse;, the code will not compile due to unknown type.
Final Answer:
Missing import of Reverse from std::cmp -> Option A
Quick Check:
Reverse requires import = C [OK]
Hint: Always import Reverse to use it with BinaryHeap [OK]
Common Mistakes:
Forgetting to import Reverse
Assuming BinaryHeap supports min heap natively
Expecting peek() to return smallest without Reverse
5. You want to efficiently find the top 3 smallest numbers from a large stream of integers in Rust. Which approach is best?
hard
A. Sort all numbers and pick first 3 after processing.
B. Use a min heap of size 3 to keep track of smallest numbers seen so far.
C. Use a max heap of size 3 to keep track of smallest numbers seen so far.
D. Use a max heap of all numbers and pop 3 times.
Solution
Step 1: Understand problem goal
We want the top 3 smallest numbers from a large stream efficiently without storing all numbers.
Step 2: Choose heap type and size
Using a max heap of size 3 keeps the current smallest 3 numbers; if a new number is smaller than the max, replace it.
Step 3: Evaluate other options
Min heap of size 3 would keep largest 3 numbers, sorting all is inefficient, max heap of all numbers wastes memory.
Final Answer:
Use a max heap of size 3 to keep track of smallest numbers seen so far. -> Option C
Quick Check:
Max heap size 3 tracks smallest top 3 [OK]
Hint: Use max heap to track smallest k elements efficiently [OK]
Common Mistakes:
Using min heap for smallest k elements incorrectly
Sorting entire stream wastes memory/time
Using max heap of all elements wastes memory
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.