5

I am wondering is there any built-in module or package that I could use for implementing the queue in javascript when I am coding on leetcode. As you know, it is not possible to use much time to implement a queue by hands during interview. When I was using python, I always like to use a module called collections which includes a class deque. But after browsing on stack overflow, I found most of answers are telling people how to implement a queue in javascript from scratch. I am looking for such a convenient way to implement it. Could someone help?

Hmmmmmmmm, there seems to be not a better way for implementing a queue than just using Arrays. It seems to be based on the javascript engine itself. This is a link about it: time complexity of unshift() vs. push() in Javascript

Aurora
  • 423
  • 2
  • 5
  • 12

2 Answers2

7

A queue is a FIFO structure, where the first inserted element in the list is the first one to be taken off.

In JavaScript, you can easily use Arrays in order to implement this logic.

The shift method returns and removes the first element of the array (as dequeue does), so if you add elements using push, and remove elements with shift, you are actually using a queue.

Below is an example:

const a = [];
a.push(3);
a.push(5);
a.push(7);

console.log(a.shift());

In the same way, you can have a FIFO even using unshift for adding at the begin of the array and pop for removing from the end of the array.

The result is always a FIFO structure where the first element you have added, is the first one to be taken off:

const a = [];
a.unshift(3);
a.unshift(5);
a.unshift(7);

console.log(a.pop());

While implementing a stack in JavaScript could be done in O(1) with simple arrays, through push and pop which take O(1), the queue implemented as above should take O(1) for inserting and O(n) in the worst case for removals.

A better time complexity approach that you could take, which should let you implement a queue in O(1) for both insertions and removals, could be done using Map, as below:

class MyQueue extends Map {
  constructor() {
    super();
    this.insertionIndex = 0;
    this.removalIndex = 0;
  }

  queue(element) {
    this.set(this.insertionIndex, element);
    this.insertionIndex++;
  }

  dequeue() {
    const el = this.get(this.removalIndex);
    if (typeof el !== 'undefined') {
      this.delete(this.removalIndex);
      this.removalIndex++;
    }
    return el;
  }
}

const q = new MyQueue();
q.queue(1);
q.queue(2);
console.log(q.dequeue());
console.log(q.dequeue());
q.queue(3);
console.log(q.dequeue());
console.log(q.dequeue()); // now is empty so dequeue will return undefined with no errors
q.queue(4);
console.log(q.dequeue());
quirimmo
  • 9,800
  • 3
  • 30
  • 45
  • 2
    Thanks for your answer. But as far as I know, if I implement a queue based on array, it could not provide me with O(1) time complexity when I am pushing onto or popping off elements from the head of that queue. – Aurora Dec 29 '18 at 02:44
  • I think during interview, it definitely could not satisfy the time complexity requirement. – Aurora Dec 29 '18 at 02:45
  • 1
    As far as I know, the time complexity for inserting an element at the end and removing an element from the end of an array in JS is O(1) (so `push` and `pop`). The worst case for `shift`, and `unshift` should be O(n). But I hope someone could post some more relevant information about it. About the interview, I got a similar question once about a stack (LIFO) in js, and I did use arryas with `unshift` and `pop` methods and they were satisfied by it. In JS you just have objects, even arrays are objects. There is not a better data structure you could use even implementing it by zero – quirimmo Dec 29 '18 at 02:48
  • Okay, I got it. Thanks for sharing. – Aurora Dec 29 '18 at 02:52
  • sorry for above I meant lifo with `push` and `pop` of course – quirimmo Dec 29 '18 at 03:00
  • Thank you. I believe the check for existing element in `dequeue()` should rather be `if (typeof el !== "undefined") {`. Also the `delete` should happen in that check. – foudfou Jul 09 '19 at 22:06
  • @foudfou thanks for pointing it out, you are absolutely right, was not focusing so much on the safe checks when did write the answer, more on the logic behind it. Edited! Cheers :) – quirimmo Jul 09 '19 at 22:32
1

For Breath First Search. Rotate 2 array(s) might help. Average O(1) time for FIFO.

Charlie 木匠
  • 2,234
  • 19
  • 19