Data Structures: Create a Circular Queue : last test fail

Tell us what’s happening:

Hello.

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

My log information tell me otherwise. Write is not modified by dequeue, read is not modified when returning null. is there something i am missing ? can i have the condition the test case check again ?

Your code so far


class CircularQueue {
constructor(size) {

 this.queue = new Array(size).fill(null);
 this.read = 0;
 this.write = 0;
 this.max = size - 1;

 console.log(`--------------------------------------------------------`)
 console.log(`myqueue:${size} r:${this.read} w:${this.write} q:${this.queue}`)

}

print() {
  return this.queue;
}

enqueue(item) {
 // Only change code below this line
 if(this.queue[this.write] === null){
   this.queue[this.write] = item;
   this.write = (this.write+1)%(this.max+1);
 }
 console.log(`enqueue:${item} r:${this.read} w:${this.write} q:${this.queue}`)

 // Only change code above this line
}

dequeue() {
 // Only change code below this line
 let res = this.queue[this.read];
 if (res != null) {
   this.queue[this.read] = null;
   this.read = (this.read+1)%(this.max+1);
 }
 console.log(`dequeue:${res} r:${this.read} w:${this.write} q:${this.queue}`)
 return res;
 // Only change code above this line
}
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36.

Challenge: Create a Circular Queue

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

My log against the most complete test look like this :

myqueue:3 r:0 w:0 q:,
enqueue:17 r:0 w:1 q:17,
enqueue:32 r:0 w:2 q:17,32,
enqueue:591 r:0 w:0 q:17,32,591
dequeue:17 r:1 w:0 q:,32,591
dequeue:32 r:2 w:0 q:,591
dequeue:591 r:0 w:0 q:,
dequeue:null r:0 w:0 q:,
dequeue:null r:0 w:0 q:,
dequeue:null r:0 w:0 q:,
dequeue:null r:0 w:0 q:,
enqueue:100 r:0 w:1 q:100,

@dste The problem is that your enqueue method does not meet the requirements in the instructions.

In addition, the enqueue method should return the item you enqueued if it is successful; otherwise it will return null .

thanks a lot, I totally misread this one.

I just found the github repo : https://github.com/freeCodeCamp/freeCodeCamp/blob/master/curriculum/challenges/english/08-coding-interview-prep/data-structures/create-a-circular-queue.english.md

can i fork / pull request to add a test about this requirement ?

You want to add an extra test that just validates the enqueue method returns either the item or null? If so, I think that would be a good idea. I actually suggest creating two separate tests. One when it should return the item successfully enqueued and one when it should return null for unsuccessful enqueue.

To contribute, I suggest reading the following two documents:

1 Like