Priority Queue Introduction and Concept in DSA Typescript - Time & Space Complexity
We want to understand how the time to add or remove items from a priority queue changes as the number of items grows.
How does the queue handle tasks with different priorities efficiently?
Analyze the time complexity of the following code snippet.
class PriorityQueue {
private items: number[] = [];
enqueue(element: number) {
this.items.push(element);
this.items.sort((a, b) => b - a); // highest priority first
}
dequeue() {
return this.items.pop();
}
}
This code adds elements to a queue and keeps them sorted so the highest priority is always removed first.
- Primary operation: Sorting the array after each insertion.
- How many times: Sorting happens every time we add an element.
Sorting takes more time as the list grows because it compares many elements.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 30-50 comparisons |
| 100 | About 500-700 comparisons |
| 1000 | About 10,000-15,000 comparisons |
Pattern observation: Operations grow quickly, roughly proportional to n times log n.
Time Complexity: O(n log n)
This means adding an item takes longer as the queue grows, but not as fast as checking every pair of items.
[X] Wrong: "Adding an item is always quick because we just push it to the list."
[OK] Correct: After adding, the list must be sorted to keep priorities correct, which takes extra time.
Understanding how priority queues manage time helps you explain efficient task handling in many programs.
"What if we used a heap data structure instead of sorting the array every time? How would the time complexity change?"
Practice
Priority Queue from a regular queue?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 CQuick Check:
Priority Queue = dequeue by priority [OK]
- Confusing priority queue with FIFO queue
- Thinking priority queue sorts elements permanently
- Assuming priority queue disallows duplicates
T?Solution
Step 1: Recall TypeScript class syntax
Classes are declared with theclasskeyword 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 BQuick Check:
Generic class syntax = class Name<T> {} [OK]
- Using function instead of class for data structure
- Confusing interface with class declaration
- Incorrect generic syntax placement
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?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 DQuick Check:
Sorted ascending, dequeue removes smallest [OK]
- Assuming dequeue removes largest value
- Not sorting after enqueue
- Confusing shift() with pop()
class PriorityQueue {
private data: number[] = [];
enqueue(value: number) {
this.data.push(value);
}
dequeue() {
return this.data.pop();
}
}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 AQuick Check:
pop() removes last, not priority element [OK]
- Using pop() instead of shift() or priority removal
- Ignoring sorting or priority logic
- Assuming push() sorts automatically
dequeue() always removes the highest number efficiently?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 AQuick Check:
Sort descending + remove first = highest priority [OK]
- Sorting ascending but removing wrong end
- Not sorting and scanning entire array on dequeue
- Using linked list without priority order
Ready to prove it?
Pass the test and this pattern moves unknown
