Heap (Priority Queue) Patterns — DSA Pattern
The Heap (Priority Queue) Patterns pattern applies when you need top K elements, K-way merge, two heaps for median, and scheduling. Its time complexity is O(n log k) and space complexity O(k). It is used in 22 problems on Thita, including Finding MK Average, Find K Pairs with Smallest Sums and Find Median from Data Stream. Common variations are Two Heaps for Median Finding and Top K Elements (Selection/Frequency).
Master heap-based algorithms including top K elements, two heaps for median, K-way merge, and scheduling problems with priority queues.
Heap (Priority Queue) Patterns is pattern 9 of 16 in the Thita.ai DSA course. It covers top K elements, K-way merge, two heaps for median, and scheduling, split into 4 subpatterns and about 2 hours of study. It is one of the interview-critical patterns, so it is worth covering before your first onsite.
Subpatterns of Heap (Priority Queue) Patterns
Each subpattern below has its own theory page and problem set. They are listed in the order the course teaches them.
- Top K Elements (Selection/Frequency) — Find top K elements using heap. Solve kth largest, top K frequent, and k closest points problems. (core technique)
- K-way Merge — Master K-way merge pattern using min heap. Merge k sorted lists and find kth smallest in matrix. (core technique)
- Two Heaps for Median Finding — Master the two heaps technique to find median from data stream in O(log n) time. (important variation)
- Scheduling / Minimum Cost (Greedy with Priority Queue) — Solve scheduling and optimization problems using priority queue with greedy approach. (depth topic)
How to practise Heap (Priority Queue) Patterns on Thita.ai
Read the theory for a subpattern, then solve its problems in the browser editor. Your solution runs against the problem's test cases, and the AI coach gives a hint on the technique you are missing rather than the finished answer. Progress is tracked per subpattern, so the tracker shows which of the 4 Heap (Priority Queue) Patterns subpatterns you have covered. Topics covered here include heap, priority queue, top k elements, kth largest, k-way merge, two heaps median, min heap, max heap.
Related DSA patterns
- String Manipulation Patterns — Palindrome checking, anagram detection, pattern matching, and string conversion.
- Tree Traversal Patterns (DFS & BFS) — Preorder, inorder, level order traversal, LCA, and serialization.
- Greedy Patterns — Interval scheduling, jump games, stock trading, and task scheduling.
- Backtracking Patterns — Subsets, permutations, combinations, N-Queens, and word search.
- Dynamic Programming (DP) Patterns — Fibonacci, Kadane, knapsack, LCS, LIS, edit distance, grid paths, and word break.
- All 16 DSA patterns
- The DSA patterns sheet
- Coding practice