# freeCodeCamp Challenge Guide: Create a Priority Queue Class

**URL:** <https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-create-a-priority-queue-class/301630>\
**Category:** freeCodeCamp Contributors\
**Tags:** guide\
**Created:** [August 3, 2019, 1:26pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-create-a-priority-queue-class/301630 "2019-08-03T13:26:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![camperbot](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/camperbot/32/18364_2.png) [@camperbot](https://forum.freecodecamp.org/u/camperbot)\
**Post date:** [August 3, 2019, 1:26pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-create-a-priority-queue-class/301630/1 "2019-08-03T13:26:11Z")

</div>

# [Create a Priority Queue Class](https://www.freecodecamp.org/learn/coding-interview-prep/data-structures/create-a-priority-queue-class)

* * *

## Problem Explanation

- Priority Queue is an Abstract Data Type.
- It can be implemented using other Data Structures but is commonly implemented using a Heap.
- Each node contains a priority. When we enqueue a node to the queue, it’s “bubbled up” to its place in the queue.
- In this challenge we need to create a Priority Queue class based on Min-Heap with a few methods:
  - enqueue() - It enqueue’s the new node to it’s appropriate place in queue.
  - dequeue() - It removes the first node from the queue.
  - front() - This method returns the first node to be dequeued.
  - size() - Returns the size of the queue.
  - isEmpty() - Returns if the queue is empty.

- 

| DS | Access | Search | Insert | Delete |
| --- | --- | --- | --- | --- |
| Priority Queue | 1 | 1 | logn | logn |

### Relevant Links

- [Wikipedia](https://en.wikipedia.org/wiki/Priority_queue)

* * *

## Solutions

> **Solution 1 (Click to Show/Hide)**
>
> ```javascript
> function PriorityQueue () {
> this.collection = [];
> this.printCollection = function() {
> console.log(this.collection);
> };
> // Only change code below this line
> this.enqueue = function(item) {
> let index = this.collection.findIndex(elem => elem[1] > item[1]);
> if (index !== -1) {
> this.collection.splice(index, 0, item);
> } else {
> this.collection.push(item);
> }
> }
> 
> this.dequeue = function() {
> return this.collection.shift()[0];
> }
> 
> this.size = function() {
> return this.collection.length;
> }
> 
> this.isEmpty = function() {
> return this.size() === 0;
> }
> 
> this.front = function() {
> return this.collection[0][0];
> }
> // Only change code above this line
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 8, 2025, 3:51pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-create-a-priority-queue-class/301630/2 "2025-01-08T15:51:13Z")

</div>

Closed on January 8, 2025, 3:51pm UTC


