📖 Scenario: Imagine you are managing a hospital emergency room. Patients arrive with different levels of urgency. You want to treat the most urgent patients first. A priority queue helps you organize patients so that the highest priority patient is always treated next.
🎯 Goal: Build a simple priority queue using Rust's BinaryHeap to manage patients with different urgency levels. You will add patients, set a priority threshold, filter patients above that threshold, and print the queue.
📋 What You'll Learn
Create a priority queue with patient names and their urgency levels
Add a priority threshold variable
Filter patients with urgency above the threshold
Print the final priority queue state
💡 Why This Matters
🌍 Real World
Priority queues are used in real life to manage tasks or people based on importance, like emergency rooms, task schedulers, or network packet handling.
💼 Career
Understanding priority queues is essential for software engineers working on scheduling algorithms, real-time systems, and efficient data processing.
Progress0 / 4 steps
1
Create the initial priority queue
Create a BinaryHeap called patients and add these tuples: (5, "Alice"), (2, "Bob"), (8, "Charlie"), (1, "Diana"), (7, "Ethan"). Use std::collections::BinaryHeap and patients.push() to add each patient.
DSA Rust
Hint
Use patients.push((urgency, "Name")) for each patient.
2
Add a priority threshold
Create a variable called priority_threshold and set it to 4. This will help us filter patients with urgency above this value.
DSA Rust
Hint
Write let priority_threshold = 4; below the patient additions.
3
Filter patients above the priority threshold
Create a new BinaryHeap called filtered_patients. Use a for loop with patient to iterate over patients.clone(). Inside the loop, if patient.0 > priority_threshold, push patient into filtered_patients.
DSA Rust
Hint
Use patients.clone() to iterate without consuming the original queue.
4
Print the filtered priority queue
Use a while let loop to pop patients from filtered_patients and print each patient tuple using println!("{:?}", patient).
DSA Rust
Hint
Use while let Some(patient) = filtered_patients.pop() to print all patients.
Practice
(1/5)
1. What is the main feature of a Priority Queue compared to a regular queue?
easy
A. It only stores elements of the same value
B. Elements are always dequeued in the order they were inserted
C. Elements are dequeued based on priority, not just order of insertion
D. It does not allow duplicate elements
Solution
Step 1: Understand queue behavior
A regular queue removes elements in the order they were added (FIFO).
Step 2: Understand priority queue behavior
A priority queue removes elements based on their priority, not just insertion order.
Final Answer:
Elements are dequeued based on priority, not just order of insertion -> Option C
Quick Check:
Priority Queue = Priority-based removal [OK]
Hint: Priority queue removes highest priority first, not oldest [OK]
Common Mistakes:
Confusing priority queue with FIFO queue
Thinking priority queue sorts elements permanently
Assuming priority queue disallows duplicates
2. Which of the following is the correct way to create a max-heap priority queue in Rust using std::collections::BinaryHeap?
easy
A. let mut pq = std::collections::BinaryHeap::min_heap();
B. let mut pq = std::collections::BinaryHeap::with_capacity(10i32);
C. let mut pq = std::collections::BinaryHeap::default(10);
D. let mut pq = std::collections::BinaryHeap::new();
Solution
Step 1: Recall Rust BinaryHeap creation
The standard way to create a max-heap priority queue is using BinaryHeap::new().
Step 2: Check other options
with_capacity(10i32) type mismatch (usize expected); default(10) invalid arguments; min_heap() does not exist.
Final Answer:
let mut pq = std::collections::BinaryHeap::new(); -> Option D
Quick Check:
BinaryHeap::new() creates max-heap [OK]
Hint: Use BinaryHeap::new() for max-heap priority queue [OK]
Common Mistakes:
Using with_capacity with i32 instead of usize
Passing arguments to default()
Using non-existent min_heap() method
3. What will be the output of the following Rust code using a priority queue?
use std::collections::BinaryHeap;
fn main() {
let mut pq = BinaryHeap::new();
pq.push(10);
pq.push(5);
pq.push(20);
while let Some(val) = pq.pop() {
print!("{} ", val);
}
}
medium
A. 20 10 5
B. 5 20 10
C. 10 5 20
D. 5 10 20
Solution
Step 1: Understand BinaryHeap behavior
Rust's BinaryHeap is a max-heap by default, so pop() returns the largest element first.
Step 2: Trace the pop order
Inserted values: 10, 5, 20. Popping returns 20, then 10, then 5.
Final Answer:
20 10 5 -> Option A
Quick Check:
Max-heap pop order = descending [OK]
Hint: BinaryHeap pops largest first, so output is descending [OK]
Common Mistakes:
Assuming pop returns elements in insertion order
Thinking BinaryHeap is min-heap by default
Mixing up push and pop order
4. Identify the error in this Rust code that tries to create a min-heap priority queue:
use std::collections::BinaryHeap;
fn main() {
let mut pq = BinaryHeap::new();
pq.push(3);
pq.push(1);
pq.push(2);
while let Some(val) = pq.pop() {
println!("{}", val);
}
}
medium
A. BinaryHeap does not support min-heap by default
B. pq.push() method does not exist
C. pop() returns elements in insertion order
D. println! macro is used incorrectly
Solution
Step 1: Check BinaryHeap default behavior
Rust's BinaryHeap is a max-heap by default, so it does not create a min-heap without extra work.
Step 2: Verify code correctness
push() and pop() methods exist and println! is correct; the issue is the heap type.
Final Answer:
BinaryHeap does not support min-heap by default -> Option A
Quick Check:
BinaryHeap default = max-heap, no min-heap [OK]
Hint: BinaryHeap is max-heap by default; min-heap needs wrapper [OK]
Common Mistakes:
Assuming BinaryHeap is min-heap by default
Thinking push() or pop() are missing
Misreading println! syntax
5. You want to implement a task scheduler in Rust that always runs the highest priority task first. Which approach correctly uses a priority queue to achieve this?