# freeCodeCamp Challenge Guide: Create a Queue Class

**URL:** <https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-create-a-queue-class/301631>\
**Category:** freeCodeCamp Contributors\
**Tags:** guide\
**Created:** [August 3, 2019, 1:26pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-create-a-queue-class/301631 "2019-08-03T13:26:18Z")\
**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-queue-class/301631/1 "2019-08-03T13:26:18Z")

</div>

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

* * *

## Problem Explanation

- A Queue is an abstract Data Structure.

- A Queue folow FIFO/LILO principle.

- In this challenge we nede to implement `enqueue()`, `dequeue()`, `front()`, `size()`, `isEmpty()` methods.

- 

| DS | Access | Search | Insert | Delete |
| --- | --- | --- | --- | --- |
| Queue | n | n | 1 | 1 |

- ![Queue in action](https://upload.wikimedia.org/wikipedia/commons/thumb/5/52/Data_Queue.svg/405px-Data_Queue.svg.png)

* * *

## Solutions

> **Solution 1 (Click to Show/Hide)**
>
> **Note:** This solution is not exactly a queue, the shift() method used in the dequeue() method is of complexity `O(n)` and not `O(1)`. However, the advanced solution rectifies this and uses Object(HashTables) instead of Array to implement Queue.
> 
> ```js
> function Queue() {
> var collection = [];
> this.print = function() {
> console.log(collection);
> };
> this.enqueue = function(val) {
> collection.push(val);
> };
> this.dequeue = function() {
> return collection.shift();
> };
> this.front = function() {
> return collection[0];
> };
> this.size = function() {
> return collection.length;
> };
> this.isEmpty = function() {
> return collection.length === 0;
> };
> }
> 
> ```

> **Solution 2 (Click to Show/Hide)**
>
> ```js
> class Queue {
> constructor() {
> this.collection = {};
> this.start = 0;
> this.end = 0;
> }
> print() {
> console.log(this.collection);
> }
> enqueue(val) {
> this.collection[this.end++] = val;
> }
> dequeue() {
> return this.collection[this.start++];
> }
> front() {
> return this.collection[this.start];
> }
> size() {
> return this.end - this.start;
> }
> isEmpty() {
> return this.size() === 0;
> }
> }
> 
> ```
> 
> #### Relevant Links
> 
> - [Wikipedia](https://en.wikipedia.org/wiki/Queue_(abstract_data_type))

---

<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-queue-class/301631/2 "2025-01-08T15:51:13Z")

</div>

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


