📖 Scenario: You are managing a small hospital emergency room. Patients arrive with different urgency levels. You want to treat the most urgent patients first.
🎯 Goal: Build a simple priority queue using a list of patients with their urgency levels. Then, extract patients in order of urgency.
📋 What You'll Learn
Create a list of patients with their urgency levels
Add a variable to hold the number of patients
Sort patients by urgency level using a simple method
Print the patients in order from highest to lowest urgency
💡 Why This Matters
🌍 Real World
Priority queues are used in hospitals, task schedulers, and anywhere urgent tasks must be handled first.
💼 Career
Understanding priority queues helps in roles like software development, system design, and operations where task prioritization is key.
Progress0 / 4 steps
1
Create the list of patients with urgency levels
Create a variable called patients as a slice of structs with fields name (string) and urgency (int). Add these exact entries: {"John", 3}, {"Alice", 5}, {"Bob", 2}, {"Diana", 4}.
DSA Go
Hint
Use a struct type Patient with fields name and urgency. Then create a slice patients with the given entries.
2
Add a variable for the number of patients
Add a variable called count and set it to the length of the patients slice using the len function.
DSA Go
Hint
Use len(patients) to get the number of patients and assign it to count.
3
Sort patients by urgency level
Use a simple bubble sort with a for loop and nested for loop to sort the patients slice in descending order by urgency. Swap patients when the next patient has higher urgency.
DSA Go
Hint
Use nested loops to compare adjacent patients and swap them if the next has higher urgency. This sorts the slice in descending order.
4
Print patients in order of urgency
Use a for loop with variable i from 0 to count to print each patient's name and urgency in the format: "Name: [name], Urgency: [urgency]".
DSA Go
Hint
Use fmt.Printf inside a loop to print each patient's name and urgency in the required format.
Practice
(1/5)
1. What is the main feature of a priority queue compared to a regular queue?
easy
A. It does not allow insertion after creation
B. Elements are always dequeued in the order they were inserted
C. Elements are dequeued based on their priority, not just order of insertion
D. It only stores elements of the same value
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 just insertion order.
Final Answer:
Elements are dequeued based on their 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 only stores unique values
Assuming insertion order always matters
2. Which of the following is the correct way to declare a priority queue in Go using the container/heap package?
easy
A. var pq = &PriorityQueue{} // where PriorityQueue implements heap.Interface
B. var pq heap.Interface
C. pq := make([]int, 0) // simple slice, no heap
D. var pq = new(priorityQueue) // without implementing heap.Interface
Solution
Step 1: Recognize Go heap usage
Go's container/heap requires a type implementing heap.Interface.
Step 2: Correct declaration
Creating a pointer to a struct implementing heap.Interface is correct.
Final Answer:
var pq = &PriorityQueue{} // where PriorityQueue implements heap.Interface -> Option A
heap.Pop requires a pointer to the heap, but code calls heap.Pop(h) passing value.
Step 2: Confirm other methods
Push, Less, Swap are correctly defined. The error is in calling heap.Pop without &.
Final Answer:
heap.Pop called with value instead of pointer -> Option A
Quick Check:
heap.Pop needs pointer argument [OK]
Hint: heap.Pop needs pointer, not value [OK]
Common Mistakes:
Calling heap.Pop with value instead of pointer
Confusing Less method for error
Ignoring method signatures for Push/Pop
5. You want to implement a task scheduler using a priority queue in Go, where tasks with lower integer priority values run first. Which of the following Less method implementations correctly creates a min-heap for this purpose?