Why am I failing the dequeue test for Create a Circular Queue?

Why am I failing the dequeue test for Create a Circular Queue?
0.0 0

#1

Tell us what’s happening:
I’m failing “the dequeue method dequeues items from the queue” and “Trying to dequeue past the write pointer returns null and does not advance the write pointer”, but passing the other three tests including “After an item is dequeued its position in the queue should be reset to null” (which implies items ARE being dequeued).

I’ve tried to use console.log to see if I can find a point where the logic is flawed, but the read counter is being advanced and the items are being removed after being stored in the item variable. Am I missing some logic failure, or is there some unwritten requirements to pass these tests, like the dequeue has to happen in the return line, and/or I need to explicitly compare the read/write positions?

Your code so far


class CircularQueue {
   constructor(size) {

     this.queue = [];
     this.read = 0;
     this.write = 0;
     this.max = size - 1;

     while (size > 0) {
        this.queue.push(null);
        size--;
     }

   }

   print() {
     return this.queue;
   }


   enqueue(item) {
    // Only change code below this line
    if (this.queue[this.write] === null) {
      this.queue.splice(this.write, 1, item);
      if (this.write < this.queue.length - 1) {
        this.write++;
      } else {
        this.write = 0;
      }
      return item;
    } else {
      return null;
    }
    // Only change code above this line
   }

   dequeue() {
    // Only change code below this line
    if (this.queue[this.read] !== null) { //if current position has an item
      let item = this.queue.splice(this.read, 1, null); //then get the item and replace it with null
      if (this.read < this.queue.length - 1) { // if read counter is not in the last position
        this.read++; //advance read counter by 1
      } else { // if read counter is in last position
        this.read = 0; //reset read counter to start position
      }
      return item; //now that read counter has been updated, can return the item being dequeued
    } else { // if current position has no item, we are trying to dequeue past the write
      return null; // we can't read past what's written, so can't dequeue and return null
    }

    // Only change code above this line
   }
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0.

Link to the challenge:
https://learn.freecodecamp.org/coding-interview-prep/data-structures/create-a-circular-queue


#2

Does the splice() really returns the item that has been discarded?


#3

Not sure if this was a Socratic question or not, but it did help me solve it: splice returns the item(s) in an array, I should have used “return: item[0];” instead of “return: item”
(or “let item = this.queue.splice(this.read, 1, null)[0];” is more accurate from a self-documentation perspective)


#4

Well, when dequeing an item from queue, people usually expect it to return the actual item rather than the subqueue containing the item. So, it is not technically the same as the item that was in the queue.