Another problem with a challenge

The challenge is:

Coding Interview Prep > Data Structures > Create a Circular Queue

My solution is:

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[this.write] = item;
      if (this.write == this.max) {
        this.write = 0;
      } else {
        this.write++;
      }
    }
    // Only change code above this line
  }

  dequeue() {
    // Only change code below this line
    let result = "";
    if (this.queue[this.read] != null) {
      result = this.queue[this.read];
      this.queue[this.read] = null;
      if (this.read == this.max) {
        this.read = 0;
      } else {
        this.read++;
      }
      return result;
    } else {
      return null;
    }
    // Only change code above this line
  }
}

// tests
let cq = new CircularQueue(5);
let dequeued;
for (let i = 0; i <= 100; i++) {
  cq.enqueue(i + 2 + "b");
  console.log("enqueue")
  console.log(cq.print())
  console.log("write=> ", cq.write, " read=> ", cq.read);
  console.log("")

  cq.enqueue(i + 3 + "c");
  console.log("enqueue")
  console.log(cq.print())
  console.log("write=> ", cq.write, " read=> ", cq.read);
  console.log("")

  dequeued = cq.dequeue();
  console.log("dequeue")
  console.log(cq.print())
  console.log("write=> ", cq.write, " read=> ", cq.read, " dequeued=> ", dequeued);
  console.log("")

  dequeued = cq.dequeue();
  console.log("dequeue")
  console.log(cq.print())
  console.log("write=> ", cq.write, " read=> ", cq.read, " dequeued=> ", dequeued);
  console.log("")

  dequeued = cq.dequeue();
  console.log("dequeue")
  console.log(cq.print())
  console.log("write=> ", cq.write, " read=> ", cq.read, " dequeued=> ", dequeued);
  console.log("")
}

It seems to me, looking at the output of the test code, that the code is working fine. But the system tests return the following error:

Trying to dequeue past the write pointer should return null
and does not advance the write pointer.

I repeat, looking at the test code it seems to me that this requirement is met: trying to dequeue past the write pointer it returns null and does not advance the write pointer. But obviously there is an error that I cannot see.

Thanks for your patience.

this is the test:

assert(
  (function () {
    var test = new CircularQueue(3);
    test.enqueue(17);
    test.enqueue(32);
    test.enqueue(591);
    return (
      test.dequeue() === 17 &&
      test.dequeue() === 32 &&
      test.dequeue() === 591 &&
      test.dequeue() === null &&
      test.dequeue() === null &&
      test.dequeue() === null &&
      test.dequeue() === null &&
      test.enqueue(100) === 100 &&
      test.dequeue() === 100
    );
  })()
);

( freeCodeCamp/create-a-circular-queue.md at main · freeCodeCamp/freeCodeCamp · GitHub )
you could find out now what’s the issue here

Many thanks.

I rewrote the tests based on the assertion you gave me.

I report the code.

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[this.write] = item;
      if (this.write == this.max) {
        this.write = 0;
      } else {
        this.write++;
      }
    }
    // Only change code above this line
  }

  dequeue() {
    // Only change code below this line
    let result = "";
    if (this.queue[this.read] != null) {
      result = this.queue[this.read];
      this.queue[this.read] = null;
      if (this.read == this.max) {
        this.read = 0;
      } else {
        this.read++;
      }
      return result;
    } else {
      return null;
    }
    // Only change code above this line
  }
}

// tests
let cq = new CircularQueue(3);
let dequeued;
cq.enqueue(17);
  console.log("enqueue")
  console.log(cq.print())
  console.log("write=> ", cq.write, " read=> ", cq.read);
  console.log("")

cq.enqueue(32);
  console.log("enqueue")
  console.log(cq.print())
  console.log("write=> ", cq.write, " read=> ", cq.read);
  console.log("")

cq.enqueue(591);
  console.log("enqueue")
  console.log(cq.print())
  console.log("write=> ", cq.write, " read=> ", cq.read);
  console.log("")

dequeued = cq.dequeue();
  console.log("dequeue")
  console.log(cq.print())
  console.log("write=> ", cq.write, " read=> ", cq.read, " dequeued=> ", dequeued);
  console.log("")

dequeued = cq.dequeue();
  console.log("dequeue")
  console.log(cq.print())
  console.log("write=> ", cq.write, " read=> ", cq.read, " dequeued=> ", dequeued);
  console.log("")

dequeued = cq.dequeue();
  console.log("dequeue")
  console.log(cq.print())
  console.log("write=> ", cq.write, " read=> ", cq.read, " dequeued=> ", dequeued);
  console.log("")

dequeued = cq.dequeue();
  console.log("dequeue")
  console.log(cq.print())
  console.log("write=> ", cq.write, " read=> ", cq.read, " dequeued=> ", dequeued);
  console.log("")

dequeued = cq.dequeue();
  console.log("dequeue")
  console.log(cq.print())
  console.log("write=> ", cq.write, " read=> ", cq.read, " dequeued=> ", dequeued);
  console.log("")

dequeued = cq.dequeue();
  console.log("dequeue")
  console.log(cq.print())
  console.log("write=> ", cq.write, " read=> ", cq.read, " dequeued=> ", dequeued);
  console.log("")

dequeued = cq.dequeue();
  console.log("dequeue")
  console.log(cq.print())
  console.log("write=> ", cq.write, " read=> ", cq.read, " dequeued=> ", dequeued);
  console.log("")

cq.enqueue(100);
  console.log("enqueue")
  console.log(cq.print())
  console.log("write=> ", cq.write, " read=> ", cq.read);
  console.log("")

dequeued = cq.dequeue();
  console.log("dequeue")
  console.log(cq.print())
  console.log("write=> ", cq.write, " read=> ", cq.read, " dequeued=> ", dequeued);
  console.log("")

I report the results.

enqueue
[ 17, null, null ]
write=>  1  read=>  0

enqueue
[ 17, 32, null ]
write=>  2  read=>  0

enqueue
[ 17, 32, 591 ]
write=>  0  read=>  0

dequeue
[ null, 32, 591 ]
write=>  0  read=>  1  dequeued=>  17

dequeue
[ null, null, 591 ]
write=>  0  read=>  2  dequeued=>  32

dequeue
[ null, null, null ]
write=>  0  read=>  0  dequeued=>  591

dequeue
[ null, null, null ]
write=>  0  read=>  0  dequeued=>  null

dequeue
[ null, null, null ]
write=>  0  read=>  0  dequeued=>  null

dequeue
[ null, null, null ]
write=>  0  read=>  0  dequeued=>  null

dequeue
[ null, null, null ]
write=>  0  read=>  0  dequeued=>  null

enqueue
[ 100, null, null ]
write=>  1  read=>  0

dequeue
[ null, null, null ]
write=>  1  read=>  1  dequeued=>  100

It seems to me that the results are correct, nevertheless the system tests still return the error

Trying to dequeue past the write pointer should
return null and does not advance the write
pointer.

(maybe I’m definitely wrong, but it seems to me that the solution found on github is wrong …)

Take a closer look at the specification of enqueue method, there’s one item that’s missing in the implementation.

I’m sorry, but I still can’t see the error. I rewrote the tests to insert all the assertions expected by the problem and found on github. I report only the tests, the code has not changed.

// tests
console.log("Assert 1");
var test = new CircularQueue(3);
test.enqueue(17);
test.enqueue(32);
test.enqueue(591);
console.log(test.print());
// shoul be
// print[0] === 17
// print[1] === 32
// print[2] === 591
// it is
console.log("");

console.log("Assert 2");
var test = new CircularQueue(3);
test.enqueue(17);
test.enqueue(32);
test.enqueue(591);
test.enqueue(13);
test.enqueue(25);
test.enqueue(59);
console.log(test.print());
// should be
//print[0] === 17
// print[1] === 32
// print[2] === 591;
// it is
console.log("");

console.log("Assert 3");
var test = new CircularQueue(3);
test.enqueue(17);
test.enqueue(32);
test.enqueue(591);
var a = test.dequeue();
var b = test.dequeue();
var c = test.dequeue()
console.log("a: ", a, "b: ", b, "c: ", c);
// should be
// a === 17
// b === 32
// c === 591
// it is
console.log("");

console.log("Assert 4");
var test = new CircularQueue(3);
test.enqueue(17);
test.enqueue(32);
test.enqueue(672);
test.dequeue();
test.dequeue();
console.log(test.print());
// should be
// print[0] === null
// print[1] === null
// print[2] === 672;
// it is
console.log("");

console.log("Assert 5");
var test = new CircularQueue(3);
test.enqueue(17);
test.enqueue(32);
test.enqueue(591);
var a = test.dequeue();
var b = test.dequeue();
var c = test.dequeue();
var d = test.dequeue();
var e = test.dequeue();
var f = test.dequeue();
var g = test.dequeue();
test.enqueue(100)
var h = test.dequeue();
console.log("a: ", a, "b: ", b, "c: ", c, "d: ", d, "e: ", e, "f: ", f, "g: ", g, "h: ", h);
// should be
// a === 17
// b === 32 
// c === 591
// d === null
// e === null
// f === null
// g === null
// h === 100
// it is

Here are the results, all correct.

Assert 1
[ 17, 32, 591 ]

Assert 2
[ 17, 32, 591 ]

Assert 3
a:  17 b:  32 c:  591

Assert 4
[ null, null, 672 ]

Assert 5
a:  17 b:  32 c:  591 d:  null e:  null f:  null g:  null h:  100

If the results are all correct the challenge should be passed.

Can you let me know what else the problem wants?

Notice that what test.enqueue(100) returns is also checked.

I passed the challenge. But the error was not the one indicated by the system tests.

Thanks to all of you.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.