A Priority Queue is a special type of queue where each element has a priority. Elements with higher priority are served before elements with lower priority, unlike a normal queue which serves elements in the order they arrive.
Click to reveal answer
beginner
How does a Priority Queue differ from a regular Queue?
In a regular queue, elements are served in the order they arrive (First-In-First-Out). In a Priority Queue, elements are served based on their priority, not just arrival order.
Click to reveal answer
beginner
What are common operations of a Priority Queue?
Common operations include: 1. Insert (add an element with priority) 2. Extract (remove the element with highest priority) 3. Peek (view the element with highest priority without removing it)
Click to reveal answer
intermediate
Which data structure is often used to implement a Priority Queue efficiently?
A Priority Queue is often implemented using a Heap (usually a binary heap) because it allows fast insertion and extraction of the highest priority element.
Click to reveal answer
beginner
Why is a Priority Queue useful in real life? Give an example.
Priority Queues help manage tasks where some are more urgent than others. For example, in a hospital emergency room, patients with more serious conditions are treated before others, regardless of arrival time.
Click to reveal answer
What determines the order of elements in a Priority Queue?
AThe order elements were added
BThe priority of elements
CThe size of elements
DThe alphabetical order of elements
✗ Incorrect
In a Priority Queue, elements are ordered by their priority, not by arrival order or size.
Which operation removes the element with the highest priority from a Priority Queue?
AInsert
BSort
CExtract
DPeek
✗ Incorrect
Extract removes and returns the element with the highest priority.
What data structure is commonly used to implement a Priority Queue efficiently?
AHeap
BStack
CLinked List
DArray
✗ Incorrect
A Heap allows fast insertion and extraction of the highest priority element, making it ideal for Priority Queues.
In a Priority Queue, if two elements have the same priority, what usually decides which one is served first?
AThe one with smaller value
BRandom choice
CThe one added last
DThe one added first
✗ Incorrect
Usually, if priorities are equal, the element added first is served first (stable priority queue).
Which real-life scenario best illustrates a Priority Queue?
APatients treated in an emergency room based on severity
BBooks stacked on a shelf
CPeople lining up at a ticket counter
DCars waiting at a traffic light
✗ Incorrect
Patients treated based on severity is a classic example of a Priority Queue.
Explain what a Priority Queue is and how it works.
Think about how tasks with different urgencies are handled.
You got /4 concepts.
Describe how a Priority Queue can be implemented and why a heap is a good choice.
Consider how to quickly find and remove the highest priority element.
You got /4 concepts.
Practice
(1/5)
1. What is the main feature that distinguishes a Priority Queue from a regular queue?
easy
A. It does not allow duplicate elements
B. Elements are always dequeued in the order they were inserted
C. Elements are dequeued based on their priority, not just order of insertion
D. It only stores elements of the same value
Solution
Step 1: Understand queue behavior
A regular queue removes elements in the order they were added (FIFO).
Step 2: Identify priority queue behavior
A priority queue removes elements based on their priority, not just insertion order.
Final Answer:
Elements are dequeued based on their priority, not just order of insertion -> Option C
Quick Check:
Priority Queue = dequeue by priority [OK]
Hint: Priority queues remove highest priority first, not oldest [OK]
Common Mistakes:
Confusing priority queue with FIFO queue
Thinking priority queue sorts elements permanently
Assuming priority queue disallows duplicates
2. Which of the following is the correct TypeScript syntax to declare a priority queue class with a generic type T?
easy
A. let PriorityQueue = <T>() => { }
B. class PriorityQueue<T> { constructor() {} }
C. function PriorityQueue<T>() { }
D. interface PriorityQueue<T> { new(): T }
Solution
Step 1: Recall TypeScript class syntax
Classes are declared with the class keyword and generics use angle brackets after the class name.
Step 2: Check each option
class PriorityQueue<T> { constructor() {} } correctly declares a generic class with constructor. Others use function, arrow function, or interface incorrectly for class declaration.
Final Answer:
class PriorityQueue<T> { constructor() {} } -> Option B
Quick Check:
Generic class syntax = class Name<T> {} [OK]
Hint: Use class with <T> for generics in TypeScript [OK]
Common Mistakes:
Using function instead of class for data structure
Confusing interface with class declaration
Incorrect generic syntax placement
3. Consider this TypeScript code using a simple priority queue that stores numbers with lower values having higher priority:
class PriorityQueue {
private data: number[] = [];
enqueue(value: number) {
this.data.push(value);
this.data.sort((a, b) => a - b);
}
dequeue() {
return this.data.shift();
}
}
const pq = new PriorityQueue();
pq.enqueue(5);
pq.enqueue(1);
pq.enqueue(3);
const first = pq.dequeue();
const second = pq.dequeue();
What are the values of first and second?
medium
A. [5, 1]
B. [5, 3]
C. [3, 5]
D. [1, 3]
Solution
Step 1: Trace enqueue operations
Values 5, 1, 3 are added. After each enqueue, array is sorted ascending: [5], then [1,5], then [1,3,5].
Step 2: Trace dequeue operations
Dequeue removes first element (lowest value): first = 1, second = 3.
Final Answer:
[1, 3] -> Option D
Quick Check:
Sorted ascending, dequeue removes smallest [OK]
Hint: Sort ascending, dequeue removes first element [OK]
Common Mistakes:
Assuming dequeue removes largest value
Not sorting after enqueue
Confusing shift() with pop()
4. The following TypeScript code for a priority queue has a bug. What is the bug?
A. Dequeue removes the last element, not the highest priority
B. Enqueue does not add elements to the queue
C. Data array is not initialized
D. Class is missing a constructor
Solution
Step 1: Analyze enqueue method
Enqueue adds elements to the end of the array correctly.
Step 2: Analyze dequeue method
Dequeue uses pop(), which removes last element, not necessarily highest priority.
Final Answer:
Dequeue removes the last element, not the highest priority -> Option A
Quick Check:
pop() removes last, not priority element [OK]
Hint: Dequeue should remove highest priority, not last element [OK]
Common Mistakes:
Using pop() instead of shift() or priority removal
Ignoring sorting or priority logic
Assuming push() sorts automatically
5. You want to implement a priority queue in TypeScript where higher numbers have higher priority. Which approach correctly maintains the queue so that dequeue() always removes the highest number efficiently?
hard
A. Store elements in an array and sort descending after each enqueue, then remove first element on dequeue
B. Store elements in an array and sort ascending after each enqueue, then remove first element on dequeue
C. Store elements in an unsorted array and remove the maximum element by scanning on each dequeue
D. Store elements in a linked list without sorting and remove the first element on dequeue
Solution
Step 1: Understand priority requirement
Higher numbers have higher priority, so dequeue should remove the largest number.
Step 2: Evaluate each approach
Store elements in an array and sort ascending after each enqueue, then remove first element on dequeue removes the smallest (lowest priority). Store elements in an array and sort descending after each enqueue, then remove first element on dequeue places largest first for efficient removal. Store elements in an unsorted array and remove the maximum element by scanning on each dequeue is correct but inefficient O(n). Store elements in a linked list without sorting and remove the first element on dequeue follows insertion order, not priority.
Final Answer:
Store elements in an array and sort descending after each enqueue, then remove first element on dequeue -> Option A
Quick Check:
Sort descending + remove first = highest priority [OK]
Hint: Sort descending, dequeue removes first for highest priority [OK]
Common Mistakes:
Sorting ascending but removing wrong end
Not sorting and scanning entire array on dequeue
Using linked list without priority order
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.