Top K Elements & Priority Queues

Data Structures

Tracking extremes, medians, and K largest/smallest elements in dynamic streams

194 problems·15 Easy·105 Medium·74 Hard
Pattern Study Guide & Cheat Sheet
▼

Dynamically maintain the K extreme elements using a binary heap, turning an O(N log N) full sort into O(N log K) time.

Core Invariant: The Heap Inversion Principle: To maintain the top K LARGEST elements, use a MIN-heap of size K! The heap root holds the smallest of the top K; if a new element is larger than root, pop root and push new.

Recognize it (Keywords & Signals)
  • Find K-th largest or K-th smallest element in array or stream
  • Top K frequent elements or most frequent words
  • Merge K sorted linked lists / arrays
  • Find median from data stream (dual heap: max-heap + min-heap)
  • Task scheduler / meeting rooms
When NOT to use

K is very close to N (quickselect or full sort is faster and simpler), or random indexed lookups are needed.

How to solve (Step-by-step)
  1. 1.To find top K largest: maintain `min_heap = []` (in Python `heapq` is a min-heap by default).
  2. 2.Iterate through elements or frequency counts.
  3. 3.Push onto heap: `heapq.heappush(heap, item)`.
  4. 4.If `len(heap) > k`: pop smallest element: `heapq.heappop(heap)`.
  5. 5.Heap now holds the K largest elements; root `heap[0]` is the K-th largest.
Watch for (Interview Traps)
  • Using a max-heap for top K largest (requires pushing all N elements taking O(N log N) space and time)
  • Python's `heapq` is MIN-heap only; negate numbers `(-val)` to emulate max-heap behavior
  • Tuple comparison tie-breakers: if frequencies match, Python compares the next tuple element (ensure elements are comparable)
Top K Frequent Elements (Min-Heap of size K)
import heapq
from collections import Counter

def top_k_frequent(nums: list[int], k: int) -> list[int]:
    freq_map = Counter(nums)
    # Min-heap stores tuples: (frequency, number)
    min_heap = []
    
    for num, count in freq_map.items():
        heapq.heappush(min_heap, (count, num))
        # Keep heap size <= k so root is always the smallest among top k
        if len(min_heap) > k:
            heapq.heappop(min_heap)
            
    return [num for count, num in min_heap]
Cost
O(n log k) vs O(n log n) full sort · O(k) auxiliary memory (Significant speedup when k << n (e.g. k=10, n=1,000,000).)
Canonical problems
#215 Kth Largest Element in an Array: Min-heap of size K yields answer at root heap[0]
#347 Top K Frequent Elements: Heap sorted by frequency maintains top K elements
#295 Find Median from Data Stream: Balance max-heap (lower half) and min-heap (upper half)
#23 Merge k Sorted Lists: Min-heap of size K holds head of each list for O(N log K) merge
Medium·58 companies·Max freq 100%·Acc 66.8%
RokuDisneyYelp+55
Hard·57 companies·Max freq 100%·Acc 60.0%
NetskopeCohesityX+54
Hard·55 companies·Max freq 100%·Acc 49.0%
LINEblinkitGojek+52
Medium·51 companies·Max freq 100%·Acc 52.7%
SnapSnapSplunk+48
Hard·49 companies·Max freq 100%·Acc 54.7%
SplunkCohesityTinder+46
Medium·45 companies·Max freq 100%·Acc 69.1%
CernerPocket GemsAvito+42
Medium·28 companies·Max freq 100%·Acc 63.4%
BCGmicro1Roblox+25
Hard·26 companies·Max freq 100%·Acc 44.7%
PinterestHopperNetflix+23
Medium·24 companies·Max freq 100%·Acc 60.4%
RobinhoodBoxZynga+21
Medium·23 companies·Max freq 100%·Acc 42.2%
HTCMakeMyTripStripe+20
Medium·20 companies·Max freq 100%·Acc 69.2%
AsanaPlaidWix+17
Medium·20 companies·Max freq 100%·Acc 57.3%
RobloxTeslaAmazon+17
Medium·20 companies·Max freq 88%·Acc 49.9%
YandexCoupangCashfree+17
Medium·19 companies·Max freq 100%·Acc 65.2%
Squarepoint CapitalUBSWix+16
Medium·18 companies·Max freq 100%·Acc 57.8%
UberMolocoPhonePe+15
Medium·17 companies·Max freq 100%·Acc 45.1%
XXNeetCode 150+14
Medium·17 companies·Max freq 100%·Acc 0.4%
GopuffVisaSnowflake+14
Hard·17 companies·Max freq 100%·Acc 0.6%
Otter.aiMedia.netX+14
Medium·17 companies·Max freq 88%·Acc 75.5%
TargetWissen TechnologySquarepoint Capital+14
Easy·17 companies·Max freq 82%·Acc 66.6%
NeetCode 150NeetCode 150Nvidia+14
Medium·16 companies·Max freq 100%·Acc 51.0%
ZIPoyoSchrodinger+13
Hard·15 companies·Max freq 63%·Acc 0.5%
TikTokPinterestNutanix+12
Hard·14 companies·Max freq 100%·Acc 0.5%
YelpXX+11
Easy·13 companies·Max freq 93%·Acc 61.3%
TinderAdobeNeetCode 150+10
Hard·13 companies·Max freq 75%·Acc 73.0%
AirbnbCitadelIntuit+10
Hard·13 companies·Max freq 75%·Acc 50.0%
RobloxMongoDBPinterest+10
Medium·12 companies·Max freq 100%·Acc 61.0%
Akuna CapitalNetflixNeetCode 150+9
Medium·12 companies·Max freq 100%·Acc 0.7%
XXPhonePe+9
Medium·12 companies·Max freq 89%·Acc 56.4%
CareemZeptoLyft+9
Hard·12 companies·Max freq 69%·Acc 39.0%
SpotifyMetaDatadog+9
Hard·11 companies·Max freq 100%·Acc 70.2%
WinZOPhonePeLyft+8
Hard·11 companies·Max freq 78%·Acc 68.0%
WeRideNeetCode 150NeetCode 150+8
Medium·11 companies·Max freq 56%·Acc 42.1%
LinkedInSprinklrUber+8
Hard·10 companies·Max freq 50%·Acc 0.7%
DE ShawSalesforceAmazon+7
Hard·9 companies·Max freq 100%·Acc 0.3%
DevRevPhonePeGoldman Sachs+6
Easy·9 companies·Max freq 89%·Acc 83.6%
SamsungJPMorgan ChaseYandex+6
Medium·9 companies·Max freq 67%·Acc 63.6%
WaymoNutanixVisa+6
Medium·9 companies·Max freq 64%·Acc 49.7%
TuringPaytmVisa+6
Medium·8 companies·Max freq 100%·Acc 65.5%
WayfairTikTokGeico+5
Hard·8 companies·Max freq 100%·Acc 47.8%
CitrixPhonePeFlipkart+5
Medium·8 companies·Max freq 89%·Acc 61.8%
AsanaPalantirOracle+5
Hard·8 companies·Max freq 79%·Acc 41.6%
WaymoDE ShawUber+5
Medium·8 companies·Max freq 75%·Acc 47.8%
DoorDashIBMGoogle+5
Medium·8 companies·Max freq 62%·Acc 57.2%
ZomatoIntuitUber+5
Medium·7 companies·Max freq 100%·Acc 64.1%
McKinseyHPEVisa+4
Medium·7 companies·Max freq 100%·Acc 0.4%
QuoraSalesforceSamsung+4
Medium·7 companies·Max freq 100%·Acc 51.0%
FlexeraSalesforceAmazon+4
Medium·7 companies·Max freq 100%·Acc 46.5%
AQR Capital ManagementVisaMicrosoft+4
Medium·7 companies·Max freq 100%·Acc 0.3%
MathWorksGrouponGoldman Sachs+4
Medium·7 companies·Max freq 100%·Acc 64.0%
razorpayGrammarlyJPMorgan Chase+4
50 displayed (of 50 matching)Filtered: All Companies

Top Companies Testing Top K Elements & Priority Queues

Browse company-specific interview problem lists and recency breakdowns:

Explore All 22 DSA Coding Patterns

Master core algorithmic patterns tested in FAANG & Big Tech coding interviews.

Patterns Hub

Top K Elements & Priority Queues frequently asked questions

What is the Top K Elements & Priority Queues pattern in coding interviews?

Tracking extremes, medians, and K largest/smallest elements in dynamic streams It is categorized under Data Structures and features 194 curated interview problems across Easy (15), Medium (105), and Hard (74) difficulties.

Which tech companies frequently test Top K Elements & Priority Queues?

Companies frequently asking Top K Elements & Priority Queues questions include Google, Amazon, Meta, Microsoft, Bloomberg, Uber.

How do I master the Top K Elements & Priority Queues pattern?

Start by studying the algorithmic strategy and template code, then practice Easy and Medium problems before attempting Hard problems under timed conditions.