📖 Scenario: Imagine you are managing a hospital emergency room. Patients arrive with different levels of urgency. You want to help the most urgent patients first. This is like a priority queue where each patient has a priority number.
🎯 Goal: You will create a simple priority queue using a JavaScript array. You will add patients with their urgency levels, then remove the most urgent patient first.
📋 What You'll Learn
Create an array called patients to hold patient objects with name and priority.
Create a variable called newPatient to hold a new patient object.
Write a function called addPatient that adds newPatient to patients and sorts by priority (lowest number means highest urgency).
Write a function called treatPatient that removes and returns the patient with the highest priority (lowest number).
Print the state of patients after adding and treating patients.
💡 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 software development roles involving scheduling, resource management, and real-time systems.
Progress0 / 4 steps
1
Create the initial patient list
Create an array called patients with these exact patient objects: {name: 'Alice', priority: 4}, {name: 'Bob', priority: 2}, and {name: 'Charlie', priority: 5}.
DSA Javascript
Hint
Use square brackets [] to create an array. Each patient is an object with name and priority.
2
Add a new patient to the queue
Create a variable called newPatient and set it to the object {name: 'Diana', priority: 1}.
DSA Javascript
Hint
Use const newPatient = {name: 'Diana', priority: 1}; to create the new patient object.
3
Add and sort patients by priority
Write a function called addPatient that takes no arguments, adds newPatient to patients, and sorts patients so the patient with the lowest priority number comes first.
DSA Javascript
Hint
Use patients.push(newPatient) to add, then patients.sort((a, b) => a.priority - b.priority) to sort by priority.
4
Treat the most urgent patient and print the queue
Write a function called treatPatient that removes and returns the patient with the highest priority (lowest number) from patients. Then call addPatient(), call treatPatient(), and print the remaining patients array.
DSA Javascript
Hint
Use patients.shift() to remove the first patient (highest priority). Then print patients with console.log(patients).
Practice
(1/5)
1. What is the main difference between a Priority Queue and a regular queue?
easy
A. Elements are always dequeued in the order they were enqueued
B. Elements are dequeued based on priority, not just order of arrival
C. It only stores numbers, not other data types
D. It uses a stack internally to manage elements
Solution
Step 1: Understand queue behavior
A regular queue removes elements in the order they arrive (FIFO).
Step 2: Understand priority queue behavior
A priority queue removes elements based on their priority, not just arrival order.
Final Answer:
Elements are dequeued based on priority, not just order of arrival -> Option B
Quick Check:
Priority Queue = priority-based removal [OK]
Hint: Priority queue removes highest priority first, not oldest [OK]
Common Mistakes:
Thinking priority queue works like a normal queue
Assuming priority queue only stores numbers
Confusing priority queue with stack
2. Which of the following is the correct way to declare a priority queue class in JavaScript?
easy
A. let PriorityQueue = () => { this.items = []; }
B. function PriorityQueue() { this.items = {}; }
C. class PriorityQueue { constructor() { this.items = []; } }
D. var PriorityQueue = new Array();
Solution
Step 1: Check class syntax
class PriorityQueue { constructor() { this.items = []; } } uses correct ES6 class syntax with constructor initializing an array.
Step 2: Check other options
function PriorityQueue() { this.items = {}; } uses function but initializes an object, not array; let PriorityQueue = () => { this.items = []; } uses arrow function incorrectly for class; var PriorityQueue = new Array(); assigns array directly, not a class.
Final Answer:
class PriorityQueue { constructor() { this.items = []; } } -> Option C
Quick Check:
Correct class syntax = class PriorityQueue { constructor() { this.items = []; } } [OK]
Hint: Use ES6 class with constructor for priority queue [OK]
Common Mistakes:
Using arrow functions as classes
Initializing items as object instead of array
Assigning array directly without class
3. Consider this JavaScript code using a simple priority queue implementation:
A. It stores elements as objects instead of arrays
B. It uses shift() instead of pop() to remove elements
C. It does not initialize items array in constructor
D. It does not sort items by priority before dequeue
Solution
Step 1: Check enqueue method
Enqueue adds elements but does not sort by priority.
Step 2: Check dequeue method
Dequeue removes first element, but without sorting, priority order is not guaranteed.
Final Answer:
It does not sort items by priority before dequeue -> Option D
Quick Check:
Missing sorting causes wrong dequeue order [OK]
Hint: Sort items by priority before dequeue [OK]
Common Mistakes:
Assuming shift() removes highest priority
Ignoring sorting requirement
Confusing object vs array storage
5. You want to implement a priority queue that supports fast insertion and removal of the highest priority element. Which data structure is best suited for this, and why?
hard
A. Binary heap, because it provides efficient insert and remove operations
B. Linked list, because it maintains insertion order
C. Stack, because it removes the last inserted element quickly
D. Array, because it allows easy sorting after each insertion
Solution
Step 1: Understand operation needs
Priority queue needs fast insertion and removal of highest priority element.
Step 2: Compare data structures
Array requires sorting after insertion (slow). Linked list maintains order but slow for priority. Stack removes last inserted only. Binary heap supports insert and remove in O(log n) time efficiently.
Final Answer:
Binary heap, because it provides efficient insert and remove operations -> Option A
Quick Check:
Binary heap = efficient priority queue [OK]
Hint: Use binary heap for efficient priority queue operations [OK]
Common Mistakes:
Choosing array and ignoring sorting cost
Confusing stack with priority queue
Ignoring time complexity of operations
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.