Data Structure Priority Queue Practice Questions with Solutions

Introductions

A Priority Queue is a data structure where elements are processed according to their priority instead of simply following the order in which they were added. In a Min Priority Queue, the smallest value has the highest priority, while in a Max Priority Queue, the largest value has the highest priority. These practice questions focus on practical operations such as insertion, finding the highest-priority element, removing elements, checking the queue, and implementing a Priority Queue using JavaScript. Data Structure Priority Queue practice questions with solutions help to understand the concepts.

Question 1: Identify the Highest-Priority Element

Question

Consider this Min Priority Queue:

[10, 30, 20, 50, 40]

Which element will be removed first?

Solution

This is a Min Priority Queue.

In a Min Priority Queue:

Smaller value = Higher priority

Compare the values:

10
20
30
40
50

The smallest value is 10.

Therefore, 10 has the highest priority.

Output

10

Answer

The element 10 will be removed first.


Question 2: Identify the Highest-Priority Element in a Max Priority Queue

Question

Consider this Max Priority Queue:

[40, 90, 20, 70, 50]

Which element has the highest priority?

Solution

In a Max Priority Queue:

Larger value = Higher priority

The values are:

40, 90, 20, 70, 50

The largest value is:

90

Therefore, 90 has the highest priority.

Output

90

Answer

The element 90 will be processed first.


Question 3: Insert Elements into a Priority Queue

Question

Create a Priority Queue and insert these values:

30, 10, 50, 20

Use a Min Priority Queue where the smallest value has the highest priority.

Solution

We can represent each item using an object containing:

value
priority

JavaScript:

class PriorityQueue {
    constructor() {
        this.items = [];
    }

    enqueue(value, priority) {
        this.items.push({
            value: value,
            priority: priority
        });

        this.items.sort((a, b) => a.priority - b.priority);
    }
}

let queue = new PriorityQueue();

queue.enqueue("Task A", 30);
queue.enqueue("Task B", 10);
queue.enqueue("Task C", 50);
queue.enqueue("Task D", 20);

console.log(queue.items);

The priorities become:

10 → Task B
20 → Task D
30 → Task A
50 → Task C

Output

[
  { value: "Task B", priority: 10 },
  { value: "Task D", priority: 20 },
  { value: "Task A", priority: 30 },
  { value: "Task C", priority: 50 }
]

Answer

The queue processes items according to their priority, not their insertion order.


Question 4: Remove the Highest-Priority Element

Question

A Min Priority Queue contains:

[
    { value: "Task A", priority: 30 },
    { value: "Task B", priority: 10 },
    { value: "Task C", priority: 20 }
]

Remove the highest-priority element.

Solution

Because this is a Min Priority Queue:

Smaller priority number = Higher priority

The priorities are:

30
10
20

The highest-priority item is:

Task B → priority 10

JavaScript:

class PriorityQueue {
    constructor() {
        this.items = [];
    }

    enqueue(value, priority) {

        this.items.push({
            value: value,
            priority: priority
        });

        this.items.sort((a, b) => a.priority - b.priority);
    }

    dequeue() {
        return this.items.shift();
    }
}

let queue = new PriorityQueue();

queue.enqueue("Task A", 30);
queue.enqueue("Task B", 10);
queue.enqueue("Task C", 20);

console.log(queue.dequeue());

Output

{ value: "Task B", priority: 10 }

Answer

Task B is removed because it has the highest priority.


Question 5: Check Whether a Priority Queue is Empty

Question

Create a Priority Queue and check whether it is empty before and after inserting an element.

Solution

We can use the length property of the array.

class PriorityQueue {
    constructor() {
        this.items = [];
    }

    isEmpty() {
        return this.items.length === 0;
    }

    enqueue(value, priority) {
        this.items.push({
            value: value,
            priority: priority
        });
    }
}

let queue = new PriorityQueue();

console.log(queue.isEmpty());

queue.enqueue("Task A", 1);

console.log(queue.isEmpty());

Initially:

items.length = 0

After inserting one item:

items.length = 1

Output

true
false

Answer

The Priority Queue is empty initially and becomes non-empty after inserting an element.


Question 6: Peek at the Highest-Priority Element

Question

Create a Min Priority Queue containing:

Task A → Priority 5
Task B → Priority 1
Task C → Priority 3

Find the highest-priority element without removing it.

Solution

The smallest priority number has the highest priority.

Therefore:

Task B → Priority 1

Use peek() to view the first item without removing it.

class PriorityQueue {
    constructor() {
        this.items = [];
    }

    enqueue(value, priority) {

        this.items.push({
            value: value,
            priority: priority
        });

        this.items.sort((a, b) => a.priority - b.priority);
    }

    peek() {

        if (this.items.length === 0) {
            return null;
        }

        return this.items[0];
    }
}

let queue = new PriorityQueue();

queue.enqueue("Task A", 5);
queue.enqueue("Task B", 1);
queue.enqueue("Task C", 3);

console.log(queue.peek());

Output

{ value: "Task B", priority: 1 }

Answer

Task B has the highest priority, and peek() does not remove it from the queue.


Question 7: Process All Elements According to Priority

Question

Process these tasks using a Min Priority Queue:

Task A → Priority 4
Task B → Priority 1
Task C → Priority 3
Task D → Priority 2

Print the tasks in the order they are processed.

Solution

Sort the tasks according to priority:

Priority 1 → Task B
Priority 2 → Task D
Priority 3 → Task C
Priority 4 → Task A

JavaScript:

class PriorityQueue {
    constructor() {
        this.items = [];
    }

    enqueue(value, priority) {

        this.items.push({
            value,
            priority
        });

        this.items.sort((a, b) => a.priority - b.priority);
    }

    dequeue() {
        return this.items.shift();
    }

    isEmpty() {
        return this.items.length === 0;
    }
}

let queue = new PriorityQueue();

queue.enqueue("Task A", 4);
queue.enqueue("Task B", 1);
queue.enqueue("Task C", 3);
queue.enqueue("Task D", 2);

while (!queue.isEmpty()) {
    console.log(queue.dequeue().value);
}

Output

Task B
Task D
Task C
Task A

Answer

The processing order is:

Task B → Task D → Task C → Task A

Question 8: Handle a Priority Queue with Equal Priorities

Question

Consider this Priority Queue:

Task A → Priority 2
Task B → Priority 1
Task C → Priority 2
Task D → Priority 3

Which task has the highest priority?

Solution

The smallest priority number has the highest priority.

Compare:

Task A → 2
Task B → 1
Task C → 2
Task D → 3

The smallest priority is 1.

Therefore:

Task B

has the highest priority.

Tasks A and C have the same priority.

Output

Task B

Answer

Task B is processed first.

If two elements have the same priority, the implementation can use an additional rule, such as insertion order, to decide which one comes first.


Question 9: Implement a Max Priority Queue

Question

Create a Max Priority Queue where the larger priority number is processed first.

Insert:

Email → Priority 2
Payment → Priority 5
Message → Priority 3

Print the processing order.

Solution

For a Max Priority Queue:

Larger priority number = Higher priority

Therefore:

Payment → 5
Message → 3
Email → 2

JavaScript:

class MaxPriorityQueue {
    constructor() {
        this.items = [];
    }

    enqueue(value, priority) {

        this.items.push({
            value,
            priority
        });

        this.items.sort((a, b) => b.priority - a.priority);
    }

    dequeue() {

        if (this.items.length === 0) {
            return null;
        }

        return this.items.shift();
    }
}

let queue = new MaxPriorityQueue();

queue.enqueue("Email", 2);
queue.enqueue("Payment", 5);
queue.enqueue("Message", 3);

console.log(queue.dequeue().value);
console.log(queue.dequeue().value);
console.log(queue.dequeue().value);

Output

Payment
Message
Email

Answer

The processing order is:

Payment → Message → Email

Question 10: Find the Highest-Priority Task

Question

A hospital system stores emergency cases using priorities:

Patient A → Priority 3
Patient B → Priority 1
Patient C → Priority 4
Patient D → Priority 2

Assume 1 means the highest priority. Which patient should be handled first?

Solution

Compare the priorities:

Patient A → 3
Patient B → 1
Patient C → 4
Patient D → 2

The smallest number is 1.

Therefore:

Patient B

has the highest priority.

JavaScript:

let patients = [
    { name: "Patient A", priority: 3 },
    { name: "Patient B", priority: 1 },
    { name: "Patient C", priority: 4 },
    { name: "Patient D", priority: 2 }
];

patients.sort((a, b) => a.priority - b.priority);

console.log(patients[0].name);

Output

Patient B

Answer

Patient B should be handled first because it has the highest priority value according to the given rule.

Key Takeaways

  • A Priority Queue processes elements according to priority.
  • It does not necessarily process elements in insertion order.
  • In a Min Priority Queue, the smallest priority value is processed first.
  • In a Max Priority Queue, the largest priority value is processed first.
  • enqueue() is used to add an element.
  • dequeue() removes the highest-priority element.
  • peek() checks the highest-priority element without removing it.
  • isEmpty() checks whether the Priority Queue contains any elements.
  • Priority Queues can be implemented using arrays, linked structures, or heaps.
  • A Heap is commonly used to implement an efficient Priority Queue.
  • Priority Queues are useful in task scheduling, emergency systems, network processing, and graph algorithms.
  • When multiple elements have the same priority, an additional rule can determine their processing order.

FAQs

1. What is a Priority Queue?

A Priority Queue is a data structure where elements are processed according to their priority rather than simply according to their insertion order.

2. What is the difference between a normal Queue and a Priority Queue?

A normal Queue generally follows FIFO (First In, First Out). A Priority Queue removes the element with the highest priority first.

3. What is a Min Priority Queue?

A Min Priority Queue gives higher priority to smaller priority values.

For example:

Priority 1 → Highest
Priority 2
Priority 3 → Lowest

4. What is a Max Priority Queue?

A Max Priority Queue gives higher priority to larger priority values.

For example:

Priority 10 → Highest
Priority 5
Priority 1 → Lowest

5. What is the purpose of peek in a Priority Queue?

peek() returns the highest-priority element without removing it from the Priority Queue.

6. How is a Priority Queue commonly implemented efficiently?

A Binary Heap is commonly used because it can efficiently maintain the highest-priority element while elements are inserted and removed.

7. Where are Priority Queues used?

Priority Queues are used in task scheduling, emergency systems, CPU scheduling, network systems, Dijkstra’s algorithm, A* search, and many other applications where some elements must be processed before others.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top