A Priority Queue is a special type of queue where each element has a priority. Elements with higher priority are served before elements with lower priority, unlike a normal queue which serves elements in the order they arrive.
Click to reveal answer
beginner
How does a Priority Queue differ from a regular Queue?
In a regular queue, elements are served in the order they arrive (first-in, first-out). In a Priority Queue, elements are served based on their priority, not just arrival order.
Click to reveal answer
intermediate
What is the common data structure used to implement a Priority Queue efficiently?
A Priority Queue is often implemented using a Heap data structure, such as a binary heap, which allows quick access to the highest (or lowest) priority element.
Click to reveal answer
intermediate
In Rust, which standard library structure can be used to create a Priority Queue?
Rust's standard library provides a BinaryHeap type which can be used as a Priority Queue. It stores elements so that the largest element is always at the top by default.
Click to reveal answer
intermediate
What happens when you insert elements with the same priority into a Priority Queue?
If elements have the same priority, the Priority Queue does not guarantee the order they are served. The order depends on the implementation and is usually not stable.
Click to reveal answer
What determines the order of elements in a Priority Queue?
AThe order elements were added
BThe priority of elements
CThe size of elements
DThe type of elements
✗ Incorrect
In a Priority Queue, elements are ordered and served based on their priority.
Which data structure is commonly used to implement a Priority Queue efficiently?
ABinary Heap
BArray
CLinked List
DStack
✗ Incorrect
A Binary Heap allows quick access to the highest priority element, making it ideal for Priority Queues.
In Rust, which type provides a Priority Queue implementation?
ABinaryHeap
BLinkedList
CHashMap
DVecDeque
✗ Incorrect
Rust's BinaryHeap type is used to implement Priority Queues.
If two elements have the same priority in a Priority Queue, what can be said about their order when removed?
AThey are removed in the order they were added
BThey are removed randomly
CThey are removed based on their value
DOrder is not guaranteed and depends on implementation
✗ Incorrect
Priority Queues usually do not guarantee order for elements with the same priority.
What is the default behavior of Rust's BinaryHeap regarding element priority?
AElements are prioritized by insertion order
BSmallest element has highest priority
CLargest element has highest priority
DElements are prioritized randomly
✗ Incorrect
Rust's BinaryHeap treats the largest element as the highest priority by default.
Explain what a Priority Queue is and how it differs from a normal queue.
Think about how elements are served based on importance rather than arrival.
You got /3 concepts.
Describe how a Priority Queue can be implemented efficiently and mention the Rust type used for it.
Consider which data structure helps quickly find the highest priority element.
You got /3 concepts.
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?