📖 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 by their urgency level.
🎯 Goal: You will create a simple priority queue of patients with urgency levels, add patients to it, and then retrieve them in order of urgency.
📋 What You'll Learn
Create a priority queue of integers representing patient urgency levels
Add specific urgency levels to the priority queue
Retrieve and remove the highest priority patient urgency level
Print the state of the priority queue after operations
💡 Why This Matters
🌍 Real World
Priority queues are used in scheduling tasks, managing resources, and anywhere you need to process items by priority.
💼 Career
Understanding priority queues helps in software development roles involving algorithms, system design, and performance optimization.
Progress0 / 4 steps
1
Create a Priority Queue
Create a PriorityQueue<Integer> called patientQueue.
DSA Java
Hint
Use PriorityQueue<Integer> and name it patientQueue.
2
Add Urgency Levels to the Queue
Add the urgency levels 5, 1, 3, 4, and 2 to patientQueue using the add() method.
DSA Java
Hint
Use patientQueue.add(value) for each urgency level.
3
Retrieve and Remove the Highest Priority Patient
Use poll() on patientQueue to remove and store the highest priority urgency level in an integer variable called highestPriority.
DSA Java
Hint
Use int highestPriority = patientQueue.poll(); to get and remove the smallest urgency number.
4
Print the Highest Priority and Remaining Queue
Print the value of highestPriority and then print patientQueue to show the remaining urgency levels.
DSA Java
Hint
Use System.out.println(highestPriority); and System.out.println(patientQueue);.
Practice
(1/5)
1. What is the main characteristic of a Priority Queue compared to a regular queue?
easy
A. Elements are dequeued based on their priority, not just order of insertion
B. Elements are always dequeued in the order they were inserted
C. It only stores elements of the same value
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 priority, not insertion order.
Final Answer:
Elements are dequeued based on their priority, not just order of insertion -> Option A
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 duplicates are not allowed
2. Which of the following is the correct way to declare a Priority Queue of integers in Java?
easy
A. PriorityQueue<int> pq = new PriorityQueue<>();
B. PriorityQueue<Integer> pq = new PriorityQueue<>();
C. PriorityQueue pq = new PriorityQueue<int>();
D. PriorityQueue<Integer> pq = new PriorityQueue<int>();
Solution
Step 1: Use wrapper class for generics
Java generics require wrapper classes, so use Integer, not int.
Step 2: Correct generic syntax
Use diamond operator <> on the right side without type inside.
Final Answer:
PriorityQueue<Integer> pq = new PriorityQueue<>(); -> Option B
Quick Check:
Use wrapper Integer and diamond operator [OK]
Hint: Use wrapper classes (Integer) with generics, not primitives [OK]
Common Mistakes:
Using primitive types (int) with generics
Specifying type twice on right side
Not using diamond operator
3. What will be the output of the following Java code?
import java.util.*;
public class TestPQ {
public static void main(String[] args) {
PriorityQueue<Integer> pq = new PriorityQueue<>();
pq.add(30);
pq.add(10);
pq.add(20);
System.out.print(pq.poll() + ", " + pq.poll() + ", " + pq.poll());
}
}
medium
A. 10, 20, 30
B. 30, 20, 10
C. 20, 10, 30
D. 10, 30, 20
Solution
Step 1: Understand PriorityQueue default order
Java's PriorityQueue by default is a min-heap, so smallest element is removed first.
Step 2: Trace poll() calls
Elements added: 30, 10, 20. Poll removes 10, then 20, then 30.
Final Answer:
10, 20, 30 -> Option A
Quick Check:
Min-heap poll order = 10, 20, 30 [OK]
Hint: PriorityQueue.poll() returns smallest element first by default [OK]
Common Mistakes:
Assuming insertion order is preserved
Thinking PriorityQueue is max-heap by default
Confusing poll() with peek()
4. Identify the error in the following code snippet that uses a Priority Queue in Java:
import java.util.PriorityQueue;
public class Demo {
public static void main(String[] args) {
PriorityQueue pq = new PriorityQueue();
pq.add("apple");
pq.add(10);
System.out.println(pq.poll());
}
}
medium
A. PriorityQueue requires initial capacity in constructor
B. PriorityQueue must be imported from java.util.concurrent package
C. PriorityQueue cannot mix different data types like String and Integer
D. PriorityQueue does not support add() method
Solution
Step 1: Check PriorityQueue type safety
Without generics, PriorityQueue stores Objects but cannot compare different types.
Step 2: Mixing String and Integer causes ClassCastException
Adding both String and Integer causes runtime error when ordering elements.
Final Answer:
PriorityQueue cannot mix different data types like String and Integer -> Option C
Quick Check:
Mixed types cause runtime error in PriorityQueue [OK]
Hint: PriorityQueue elements must be mutually comparable [OK]
Common Mistakes:
Ignoring type safety and mixing types
Wrong import statement
Assuming add() method is unsupported
5. You want to create a Priority Queue in Java that always returns the largest element first. Which of the following is the best way to achieve this?
hard
A. Use PriorityQueue<Integer> pq = new PriorityQueue<>(); and add elements normally
B. Use PriorityQueue<Integer> pq = new PriorityQueue<>(null);
C. Use PriorityQueue<Integer> pq = new PriorityQueue<>(Comparator.naturalOrder());
D. Use PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
Solution
Step 1: Understand default PriorityQueue order
By default, PriorityQueue is a min-heap, so smallest element is returned first.
Step 2: Use reverse order comparator for max-heap behavior
Passing Collections.reverseOrder() makes it return largest element first.
Final Answer:
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder()); -> Option D
Quick Check:
Reverse order comparator = max-heap PriorityQueue [OK]
Hint: Use Collections.reverseOrder() to get max element first [OK]