Interview's MateDSA Visualizer
Interview's Mate

DSA Visualizer

Learn by watching algorithms run

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
All topics
DATA STRUCTURES

Heaps / Priority Queues

Always know the smallest (or largest) item, at O(log n) per update.

Intermediate9 problems2 interactive subtopicsO(n log k) time · O(k) space
Practice in AI DSA
WalkthroughOverviewRecognize itMistakes & edge casesKnowledge check (3)Practice
Algorithms & custom examples (2)Open in full playground →
STEP 1/5Fill the heap to size k
the top 2 values so far
3
2
1
5
6
4
012345
x
MIN-HEAP, SIZE ≤ 2
2
3
  • visited / done
  • active this step
  • removed / popped
  • result
Space Play←→ StepR Replay
1 / 5
PYTHONnums = [3, 2, 1, 5, 6, 4], k = 2
1import heapq
2
3def find_kth_largest(nums, k):
4 heap = []
5 for x in nums:
▶6 heapq.heappush(heap, x)
7 if len(heap) > k:
8 heapq.heappop(heap)
9 return heap[0]
STEP 1 OF 5Setup

Fill the heap to size k

Push 3 and 2. The heap holds the 2 largest values seen so far.

VARIABLES

x
2
heap
[2, 3]
02

Intuition & complexity

The core mental model, trade-offs, and Big-O cost.

What it is

A binary heap is a complete tree stored in an array where every parent is ≤ its children (a min-heap). Python’s heapq module is a min-heap.

Why it matters

Top-k, scheduling and merging problems need the next best item again and again without sorting everything.

Think of it like this

An emergency room: the most urgent patient is seen next, whoever arrived first.

Use it when

  • Top k, or the kth largest or smallest
  • Merging k sorted lists
  • Always taking the cheapest option next (Dijkstra)

Avoid it when

  • You need everything in sorted order once - just sort
  • You need fast search for arbitrary values

Complexity cheatsheet

OperationTimeSpace
Push or popO(log n)O(1)
Peek the minimumO(1)O(1)
heapify a listO(n)O(1)

Often confused with: binary search trees - they keep full order; a heap only guarantees the root is the minimum.

03

How to recognize it

Clues in interview prompts that point straight to this pattern.

kth largesttop kclosestmerge kschedule
  • A heap of size k gives O(n log k) for top-k
  • heapq is min-only - push negated values for a max-heap
04

Mistakes & edge cases

Catch common off-by-one bugs and corner cases before submitting.

Common mistakes

  • Pushing all n items into a max-heap - O(n log n) instead of O(n log k)
  • Forgetting heapq is a min-heap
  • Pushing tuples whose second item can’t be compared when the first items tie

Edge cases to test

  • k = 1 or k = n
  • Duplicates
  • Negative numbers
Interview tip: Say what heap you keep and why: “a min-heap of size k, so its root is the kth largest.”
05

Knowledge check

Quick check to lock in the intuition.

Question 1 of 3

Keeping a min-heap of size k while scanning n numbers costs…

06

Practice

Apply what you just watched on real interview problems.

Generate Heap (Priority Queue) problem in AI DSA Practice →
  • BeginnerKth Largest Element in a StreamLeetCode 703 ↗ (opens in a new tab)
  • IntermediateTop K Frequent ElementsLeetCode 347 ↗ (opens in a new tab)
  • AdvancedMerge k Sorted ListsLeetCode 23 ↗ (opens in a new tab)
Binary Search TreesGraphs