Priority Queue challenge failing

Tell us what’s happening:
This code isn’t passing the last 3 test cases. Please help.

Your code so far


function PriorityQueue () {
    this.collection = [];
    let rear = -1;
    this.printCollection = function() {
      console.log(this.collection);
    };
    // Only change code below this line
    this.enqueue = (x) => {
        if (!Array.isArray(x) || x.length !== 2) return console.log("Invalid input");
        else if (typeof this.collection !== 'undefined' && this.collection.length > 0) rear = this.collection.length - 1;
        else if (rear > this.collection.length - 1) return console.log("Queue Overflow");
        let i = 0;
        this.collection.map((item) => {
            if (item[1] > x[1]) return;
            i++;
        });
        if (i == 0) this.collection.unshift(x);
        else if (i == this.collection.length) this.collection.push(x);
        else {
            let arr = this.collection.splice(i);
            this.collection.push(x);
            this.collection = this.collection.concat(arr);
        }
    }

    this.dequeue = () => {
        if (rear <= 0) return console.log("Queue Underflow");
        rear--;
        return this.collection.shift()[0];
    }

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

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

    this.isEmpty = () => {
        return this.collection.length <= 0;
    }
    // Only change code above this line
}

Your browser information:

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

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

Few questions

  • Why are you trying to handle exceptions when you are struggling to produce a correct code?
  • There is no overflow issue when enqueueing, but you’ve just created one. Does Javascript array have static size?
  • Why are you using map() for what you are trying to do? Do you know what map() does?
  • Can you describe what enqueue() should do?
  • I put the exceptions to practise implementing basic checkpoints. Also, I feel the underflow at least is needed here.
  • I used the map function to iterate through the items in the array because a for loop wasn’t working for some reason.
  • The enqueue is to take an input, validate it, and then push it in collections such that after every insertion the queue is sorted. The way it does it is by going through all priority till a higher priority arrives, splice it from there, insert the new element, and put the spliced part back.

You generally don’t want to handle exception for your prototype. Practicing is fine, but in this case it hinders solving the actual problem. For the initial implementation assume your function receives flawless input.

Using map() to simply iterate over an array is not a good way to communicate with other coders. If you are not transforming the array prefer forEach()

You already know what you should do. If you have the correct place to insert new element, then single splice() call will just do. You’ve already mentioned it and used in your code. Perhaps you are not aware of splice() that takes at least three arguments?

After you sort these things out, you might find what went wrong. If you still can’t find it let me know.

1 Like

I’m going to sleep so I’ll just tell you what specifically went wrong.

When you enqueue() for the first time, collection exists and collection.length = 0. So, the phase for setting rear is skipped. At this point, rear = -1, which is the initial value.

Suppose you call dequeue() at this point, rear = -1 and the function misreports underflow despite having one item.

IMO, this is a consequence of mixing in unnecessary exception handling and somewhat less readable code.
As a side note, If you really wanted the underflow warning, you could’ve simply used the length of collection instead of inventing a new variable rear. You probably have known, but just in case…

1 Like