Author

Yangshun TayEx-Meta Staff Engineer

Languages

JSTS

Implement a queue data structure in JavaScript that contains the following operations:

`new Queue()`

: Creates an instance of a`Queue`

class that doesn't contain any items. The constructor does not accept any arguments.`enqueue()`

: Adds an item to the back of the queue. Required time complexity: O(1).`dequeue()`

: Removes an item from the front of the queue. Required time complexity: O(1).`isEmpty()`

: Determines if the queue is empty. Required time complexity: O(1).`front()`

: Returns the item at the front of the queue without removing it from the queue. Required time complexity: O(1).`back()`

: Returns the item at the back of the queue without removing it from the queue. Required time complexity: O(1).`length()`

: Returns the number of items in the queue. Required time complexity: O(1).

const queue = new Queue();queue.isEmpty(); // truequeue.enqueue(1);queue.enqueue(2);queue.length(); // 2queue.enqueue(3);queue.front(); // 1queue.back(); // 3queue.dequeue(); // 1queue.isEmpty(); // false

Coding and submission is not supported on mobile devices. Use a wider screen to practice solving this question within the editor.