Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

Priority Queue Introduction and Concept in DSA Javascript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Priority Queue Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Priority Queue insertion sequence?
Consider a priority queue where lower numbers have higher priority. Insert the numbers 5, 1, 3 in that order. What is the state of the queue after these insertions?
DSA Javascript
class PriorityQueue {
  constructor() {
    this.items = [];
  }
  enqueue(element) {
    if (this.isEmpty()) {
      this.items.push(element);
    } else {
      let added = false;
      for (let i = 0; i < this.items.length; i++) {
        if (element < this.items[i]) {
          this.items.splice(i, 0, element);
          added = true;
          break;
        }
      }
      if (!added) {
        this.items.push(element);
      }
    }
  }
  isEmpty() {
    return this.items.length === 0;
  }
  print() {
    return this.items.join(' -> ') + ' -> null';
  }
}

const pq = new PriorityQueue();
pq.enqueue(5);
pq.enqueue(1);
pq.enqueue(3);
console.log(pq.print());
A1 -> 3 -> 5 -> null
B5 -> 1 -> 3 -> null
C3 -> 1 -> 5 -> null
D5 -> 3 -> 1 -> null
Attempts:
2 left
💡 Hint
Remember, lower numbers have higher priority and are placed at the front.
❓ Predict Output
intermediate
2:00remaining
What is the output after dequeue operations on this Priority Queue?
Given a priority queue with elements inserted in this order: 4, 2, 6, 1 (lower number means higher priority). After inserting all, two dequeue operations are performed. What is the state of the queue?
DSA Javascript
class PriorityQueue {
  constructor() {
    this.items = [];
  }
  enqueue(element) {
    if (this.isEmpty()) {
      this.items.push(element);
    } else {
      let added = false;
      for (let i = 0; i < this.items.length; i++) {
        if (element < this.items[i]) {
          this.items.splice(i, 0, element);
          added = true;
          break;
        }
      }
      if (!added) {
        this.items.push(element);
      }
    }
  }
  dequeue() {
    return this.items.shift();
  }
  isEmpty() {
    return this.items.length === 0;
  }
  print() {
    return this.items.join(' -> ') + ' -> null';
  }
}

const pq = new PriorityQueue();
pq.enqueue(4);
pq.enqueue(2);
pq.enqueue(6);
pq.enqueue(1);
pq.dequeue();
pq.dequeue();
console.log(pq.print());
A6 -> 4 -> null
B2 -> 4 -> 6 -> null
C4 -> 6 -> null
D1 -> 2 -> 4 -> 6 -> null
Attempts:
2 left
💡 Hint
Dequeue removes the element with the highest priority (lowest number).
🧠 Conceptual
advanced
1:30remaining
Which data structure is best suited to implement a Priority Queue efficiently?
You want to implement a priority queue that supports fast insertion and fast removal of the highest priority element. Which data structure is best suited for this?
ALinked list with unsorted elements
BBinary heap
CStack
DArray sorted after every insertion
Attempts:
2 left
💡 Hint
Think about a structure that keeps partial order and allows quick access to the highest priority.
❓ Predict Output
advanced
1:30remaining
What error does this Priority Queue code produce?
Consider this code snippet for a priority queue enqueue method. What error will it raise when run? class PriorityQueue { constructor() { this.items = []; } enqueue(element) { for (let i = 0; i < this.items.length; i++) { if (element < this.items[i]) this.items.splice(i, 0, element); break; } this.items.push(element); } } const pq = new PriorityQueue(); pq.enqueue(3);
DSA Javascript
class PriorityQueue {
  constructor() {
    this.items = [];
  }
  enqueue(element) {
    for (let i = 0; i < this.items.length; i++) {
      if (element < this.items[i])
        this.items.splice(i, 0, element);
        break;
    }
    this.items.push(element);
  }
}

const pq = new PriorityQueue();
pq.enqueue(3);
ANo error, queue contains 3
BSyntaxError due to missing braces
CTypeError because splice is called incorrectly
DReferenceError for undefined variable
Attempts:
2 left
💡 Hint
Check if the loop and if statement syntax is valid.
🚀 Application
expert
2:30remaining
How many items remain after these Priority Queue operations?
A priority queue starts empty. The following operations happen in order: - enqueue(10) - enqueue(5) - enqueue(7) - dequeue() - enqueue(3) - dequeue() - dequeue() How many items remain in the queue?
A3
B0
C2
D1
Attempts:
2 left
💡 Hint
Track the queue state step-by-step, remembering dequeue removes the smallest number.

Practice

(1/5)
1. What is the main difference between a Priority Queue and a regular queue?
easy
A. Elements are always dequeued in the order they were enqueued
B. Elements are dequeued based on priority, not just order of arrival
C. It only stores numbers, not other data types
D. It uses a stack internally to manage elements

Solution

  1. Step 1: Understand queue behavior

    A regular queue removes elements in the order they arrive (FIFO).
  2. Step 2: Understand priority queue behavior

    A priority queue removes elements based on their priority, not just arrival order.
  3. Final Answer:

    Elements are dequeued based on priority, not just order of arrival -> Option B
  4. Quick Check:

    Priority Queue = priority-based removal [OK]
Hint: Priority queue removes highest priority first, not oldest [OK]
Common Mistakes:
  • Thinking priority queue works like a normal queue
  • Assuming priority queue only stores numbers
  • Confusing priority queue with stack
2. Which of the following is the correct way to declare a priority queue class in JavaScript?
easy
A. let PriorityQueue = () => { this.items = []; }
B. function PriorityQueue() { this.items = {}; }
C. class PriorityQueue { constructor() { this.items = []; } }
D. var PriorityQueue = new Array();

Solution

  1. Step 1: Check class syntax

    class PriorityQueue { constructor() { this.items = []; } } uses correct ES6 class syntax with constructor initializing an array.
  2. Step 2: Check other options

    function PriorityQueue() { this.items = {}; } uses function but initializes an object, not array; let PriorityQueue = () => { this.items = []; } uses arrow function incorrectly for class; var PriorityQueue = new Array(); assigns array directly, not a class.
  3. Final Answer:

    class PriorityQueue { constructor() { this.items = []; } } -> Option C
  4. Quick Check:

    Correct class syntax = class PriorityQueue { constructor() { this.items = []; } } [OK]
Hint: Use ES6 class with constructor for priority queue [OK]
Common Mistakes:
  • Using arrow functions as classes
  • Initializing items as object instead of array
  • Assigning array directly without class
3. Consider this JavaScript code using a simple priority queue implementation:
class PriorityQueue {
  constructor() {
    this.items = [];
  }
  enqueue(element, priority) {
    this.items.push({element, priority});
    this.items.sort((a, b) => a.priority - b.priority);
  }
  dequeue() {
    return this.items.shift().element;
  }
}

const pq = new PriorityQueue();
pq.enqueue('A', 2);
pq.enqueue('B', 1);
pq.enqueue('C', 3);
console.log(pq.dequeue());
What will be printed?
medium
A. 'B'
B. 'A'
C. 'C'
D. undefined

Solution

  1. Step 1: Analyze enqueue operations

    Elements are added with priorities: A(2), B(1), C(3). After each enqueue, array is sorted by priority ascending.
  2. Step 2: Analyze dequeue operation

    Dequeue removes first element (lowest priority number). After sorting, order is B(1), A(2), C(3). So dequeue returns 'B'.
  3. Final Answer:

    'B' -> Option A
  4. Quick Check:

    Lowest priority dequeued first = 'B' [OK]
Hint: Lowest priority number dequeued first [OK]
Common Mistakes:
  • Assuming FIFO order instead of priority
  • Confusing priority sorting order
  • Forgetting to return element, not object
4. The following code is intended to implement a priority queue, but it has a bug:
class PriorityQueue {
  constructor() {
    this.items = [];
  }
  enqueue(element, priority) {
    this.items.push({element, priority});
  }
  dequeue() {
    return this.items.shift().element;
  }
}
What is the main problem with this code?
medium
A. It stores elements as objects instead of arrays
B. It uses shift() instead of pop() to remove elements
C. It does not initialize items array in constructor
D. It does not sort items by priority before dequeue

Solution

  1. Step 1: Check enqueue method

    Enqueue adds elements but does not sort by priority.
  2. Step 2: Check dequeue method

    Dequeue removes first element, but without sorting, priority order is not guaranteed.
  3. Final Answer:

    It does not sort items by priority before dequeue -> Option D
  4. Quick Check:

    Missing sorting causes wrong dequeue order [OK]
Hint: Sort items by priority before dequeue [OK]
Common Mistakes:
  • Assuming shift() removes highest priority
  • Ignoring sorting requirement
  • Confusing object vs array storage
5. You want to implement a priority queue that supports fast insertion and removal of the highest priority element. Which data structure is best suited for this, and why?
hard
A. Binary heap, because it provides efficient insert and remove operations
B. Linked list, because it maintains insertion order
C. Stack, because it removes the last inserted element quickly
D. Array, because it allows easy sorting after each insertion

Solution

  1. Step 1: Understand operation needs

    Priority queue needs fast insertion and removal of highest priority element.
  2. Step 2: Compare data structures

    Array requires sorting after insertion (slow). Linked list maintains order but slow for priority. Stack removes last inserted only. Binary heap supports insert and remove in O(log n) time efficiently.
  3. Final Answer:

    Binary heap, because it provides efficient insert and remove operations -> Option A
  4. Quick Check:

    Binary heap = efficient priority queue [OK]
Hint: Use binary heap for efficient priority queue operations [OK]
Common Mistakes:
  • Choosing array and ignoring sorting cost
  • Confusing stack with priority queue
  • Ignoring time complexity of operations
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard