Problem with Circular Queue

Tell us what’s happening:
ohk, So I looked at the hint for this challenge, searched in this forum, but still unable to get what am I doing wrong?

It is not passing the test: Trying to dequeue past the write pointer should return null and does not advance the write pointer.

Your code so far


class CircularQueue {
constructor(size) {

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

  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) return null
  this.queue[this.write++] = item;
  this.write%=this.max;
  // Only change code above this line
}

dequeue() {
  // Only change code below this line
  if(this.queue[this.read]===null) return null;
  let item = this.queue[this.read];
  this.queue[this.read++] = null;
  this.read%=this.max;
  return item;
  // Only change code above this line
}
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36.

Challenge: Create a Circular Queue

Link to the challenge:

1 Like

There is an issue with the tests for this challenge. It’s not really a bug, but if you don’t follow the specs exactly it will get you. The same thing happened to me.

The penultimate sentence of the description says “the enqueue method should return the item you enqueued if it is successful.” I didn’t do this and the first four tests don’t check this. You didn’t do this too. However, the last test that your code is failing is series of enqueue() and dequeue() calls with checks on the return values, all anded together. The next to the last one is test.enqueue(100) === 100, and since your enqueue() method returns undefined it fails this last dequeue() test. So if you fix your return on enqueue(), it looks like your code will pass the tests.

To avoid this problem, there really needs to be a sixth test that explicitly (and only) checks the return value of the enqueue() method.

This description statement should be rewritten in points,
It’s frustrating to read the whole paragraph.:cry: