DEV Community

Steven Frasica
Steven Frasica

Posted on

Algorithm Practice: Queues

Up until I started learning about queues, most of the algorithm problems I was practicing involved arrays, strings, and objects. That said, being introduced to queues expanded my understanding of different data structures necessary for interview problems and algorithms. It helps that in my initial implementation of queues, I was taught how to use an array to create a class for a queue. Familiarity with arrays made drawing relationships between arrays and queues made understanding queues that much easier.

Queue

A queue holds data in order, and the first instance of data that goes into a queue, is also going to be the first instance of data that goes out of the queue. In Stephen Grider's Udemy course, he uses the acronym "FIFO" or "First in, first out" as an easy way to remember the process. An example that comes to mind is ordering at a sandwich shop. You go in, grab a ticket, and your ticket will be called for you to place your order after the previous tickets are called. It would be unfair if you were able to get your sandwich before the other customers who entered the queue earlier. There is no skipping in a queue. Other examples would be your music playlist (sans shuffle) or your movie streaming service queue. Once your current show or movie is over, the streaming service is already queuing up or preparing the next episode or movie to autoplay.

Implementing a Queue Data Structure

We are asked to create the queue data structure with the functionality to add and remove individual pieces of data from the queue.

We can start with using an array data structure and some array methods to build out the functionality of the queue.

class Queue {
//create an empty array
}
class Queue {
  constructor() {
    this.data = [];
  }
/* data will be an array that is a property of each instance of a queue that is created. 
The "this" keyword refers to the queue instance.
*/
}

Our next step is to create an add method for the Queue class.

class Queue {
  constructor() {
    this.data = [];
  }

  add() {
  /* add will take in an argument of some data, which we can call record. add will take the record and place it into the queue. 
*/
  }
}
class Queue {
  constructor() {
    this.data = [];
  }

  add(record) {
    this.data.unshift(record);
  }
}

The unshift array method places a new instance of data in the front of an array. So now whenever we call add with an argument on an instance of a queue, it will add a new record to the queue.

We move on to our remove method.

class Queue {
  constructor() {
    this.data = [];
  }

  add(record) {
    this.data.unshift(record);
  }

  remove() {
  // Use the array method pop to remove the last record in the array, which will allow us to remove the next record in the queue. 
  }
}
class Queue {
  constructor() {
    this.data = [];
  }

  add(record) {
    this.data.unshift(record);
  }

  remove() {
    return this.data.pop(); 
  }
  //remember to include the return keyword so it returns the queue record we just removed. 
}

Resources

Stephen Grider's Algorithms and Data Structures Udemy Course

Interview Cake

Oldest comments (0)