Data Structures - Create a Priority Queue Class

Tell us what’s happening:
Hello, I’m pretty sure this code here works, but it’s only passing the “should have” tests for some reason. Despite it functioning normally in my manual testing🤔
Is there a bug of some sorts or did I miss something?

Your code so far

function PriorityQueue () {
  this.collection = [];
  this.printCollection = function() {
    console.log(this.collection);
  };
  // Only change code below this line
this.enqueue = function(val) {
      for (let i=0;i<this.collection.length;i++){
        if(val[1]<=this.collection[i][1]){
            this.collection.splice(i,0,val);
            break;}
      }
      if(val[1]>this.collection[this.collection.length-1][1]){
        this.collection[this.collection.length]=val;
                }
    }
  
    this.dequeue = function() {
      let popped = this.collection[0];
      this.collection = this.collection.splice(1,this.collection.length);
      return popped[0];
    }
  
    this.front = function(){
      return this.collection[0][0];
    }
  
    this.size = function(){
     return this.collection.length;
    }
  
    this.isEmpty = function(){
      if (this.collection.length>0){return false;}
      else{return true;}
    }
  // Only change code above this line
}

Your browser information:

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

Challenge: Data Structures - Create a Priority Queue Class

Link to the challenge:

You say it works in manual testing, but when I did a manual test, it failed. Try adding the following two lines at the bottom:

let pq = new PriorityQueue();
pq.enqueue(["rabbit", 5]);

You should get the following error:

TypeError: Cannot read properties of undefined (reading '1')

If you can figure out the cause of this error, you will be able to pass two of the four failing tests. You will still need to fix your queue to have proper first-in-first-out order among items of the same priority.

For instance, let’s imagine we have a priority queue with three items:

[['kitten', 2], ['dog', 2], ['rabbit', 2]]

That’s from the task’s description, I’ve interpreted it as such:
pQ.enqueue([‘test’,9]); and what you said.
Also the description later says something similar to what I’ve done:
“The enqueue should accept items with the format shown above (['human', 1] )”
So not sure why the input should be as you said it to be.

The only flaw I agree with you on is the proper FIFO for items of same priority, that is not guaranteed here.

Sorry, that was a typo. I have amended it to pq.enqueue(["rabbit", 5]);

Despite fixing the FIFO problem, by just comparing values from the end to the front and adding items on the way (Guaranteeing values that have entered last are the last to exit the queue among similar priority items)
And manually testing again, still no successful tests so far

this.enqueue = function(val) {
        for (let i=this.collection.length-1;i>0;i--){
          if(val[1]>=this.collection[i][1]){
              this.collection.splice(i+1,0,val);
              break;}
        }
        if(val[1]<this.collection[0][1]){
          this.collection.splice(0,0,val);
                  }
      }

Again, I ask you to test your code. I have added a few lines of basic tests to your complete code. You can simply copy and paste the entire thing:

function PriorityQueue() {
  this.collection = [];
  this.printCollection = function() {
    console.log(this.collection);
  };
  // Only change code below this line
  this.enqueue = function(val) {
    for (let i = this.collection.length - 1; i > 0; i--) {
      if (val[1] >= this.collection[i][1]) {
        this.collection.splice(i + 1, 0, val);
        break;
      }
    }
    if (val[1] < this.collection[0][1]) {
      this.collection.splice(0, 0, val);
    }
  }

  this.dequeue = function() {
    let popped = this.collection[0];
    this.collection = this.collection.splice(1, this.collection.length);
    return popped[0];
  }

  this.front = function() {
    return this.collection[0][0];
  }

  this.size = function() {
    return this.collection.length;
  }

  this.isEmpty = function() {
    if (this.collection.length > 0) {
      return false;
    } else {
      return true;
    }
  }
  // Only change code above this line
}


/* === CUSTOM TEST CODE ===
   remove before running 
   official tests
   */
let pq = new PriorityQueue();
pq.printCollection();
pq.enqueue(["rabbit", 5]);
pq.printCollection();
pq.enqueue(["fox", 5]);
pq.printCollection();
pq.enqueue(["cat", 4]);
pq.printCollection();

Here is the expected output:

[]
[ [ 'rabbit', 5 ] ]
[ [ 'rabbit', 5 ], [ 'fox', 5 ] ]
[ [ 'cat', 4 ], [ 'rabbit', 5 ], [ 'fox', 5 ] ]

What output do you get?

Wow I guess the flu really has me impaired, I’m home sick and playing around with these challenges, but couldn’t for the life of me notice what was wrong SMH
Thanks for your help (twice!) the reason why my manual tests were working is because I was testing on an already full queue, instead of filling it value by value like your tests above, so after fixing that, it now works normally and all the tests are passed.
Thanks again!

Glad you got it sorted out! Errors often happen at edge cases, such as when removing the last item from the queue, or adding one to an empty queue, so we always need to test for that.

1 Like