Hone logo
Hone
Problems

Implementing a Priority Queue in JavaScript

Priority queues are fundamental data structures used in various applications like task scheduling, event simulation, and graph algorithms. This challenge asks you to implement a priority queue in JavaScript, allowing you to efficiently manage elements based on their assigned priorities. A priority queue ensures that the element with the highest (or lowest, depending on the implementation) priority is always readily available.

Problem Description

You are tasked with creating a PriorityQueue class in JavaScript. This class should support the following operations:

  • enqueue(element, priority): Adds a new element to the queue with the specified priority. Lower priority numbers should represent higher priority (e.g., 1 is higher priority than 2).
  • dequeue(): Removes and returns the element with the highest priority from the queue. If the queue is empty, it should return undefined.
  • peek(): Returns the element with the highest priority without removing it. If the queue is empty, it should return undefined.
  • isEmpty(): Returns true if the queue is empty, false otherwise.
  • size(): Returns the number of elements in the queue.

The implementation should maintain the priority order efficiently. Consider using an appropriate data structure (e.g., array, linked list) to achieve this.

Examples

Example 1:

Input:
queue.enqueue("Task A", 3);
queue.enqueue("Task B", 1);
queue.enqueue("Task C", 2);

Output: "Task B"
Explanation: "Task B" has the highest priority (1) and is dequeued.

Example 2:

Input:
queue = new PriorityQueue();
queue.enqueue("Task D", 5);
queue.peek();

Output: "Task D"
Explanation: "Task D" is the only element in the queue and has the highest priority (5), so it's returned by peek without being removed.

Example 3: (Edge Case - Empty Queue)

Input:
queue = new PriorityQueue();
queue.dequeue();
queue.peek();
queue.isEmpty();

Output:
undefined
undefined
true
Explanation: The queue is initially empty. Dequeue and peek return undefined, and isEmpty returns true.

Constraints

  • The priority will be a number.
  • Elements can be of any data type.
  • The queue can contain a maximum of 1000 elements.
  • Enqueue and dequeue operations should have an average time complexity of O(log n), where n is the number of elements in the queue. While a perfect O(log n) is difficult to guarantee with a simple array-based implementation, strive for efficiency.

Notes

  • Consider how you will store the elements and their priorities within the PriorityQueue class.
  • Think about how to efficiently find and remove the element with the highest priority.
  • You can choose to implement a min-heap or max-heap based priority queue. This problem assumes a min-heap (lower number = higher priority).
  • Error handling (e.g., invalid priority types) is not required for this challenge. Focus on the core functionality.
  • Test your implementation thoroughly with various scenarios, including edge cases.
Loading editor...
javascript