Skip to content
Guest
Learn DSA · Course Syllabus Show lessons
Core Data Structures · 9 min read · Intermediate

Heaps & Priority Queues (Top-K Pattern)

Extract the smallest or largest item in O(log n) using a Binary Heap to solve Top-K and scheduling problems fast.

What you'll learn

  • Understand Min-Heap / Max-Heap array indexing (parent, left, right)
  • Know Heap complexities: peek O(1), push O(log n), pop O(log n)
  • Use a size-k Min-Heap to find the Top-K largest elements in O(n log k)

A Binary Heap (Priority Queue) keeps the minimum (or maximum) element at index 0 (O(1) peek) and supports push and pop in $O(\log n)$. Because it is stored in a flat array (parent = Math.floor((i - 1) / 2)), it has zero pointer overhead.

Fill-in-the-Blank Exercise

Binary Heap Parent Index

Compute the parent index of node i in a 0-indexed binary heap.

const parent = Math.floor((i - ) / );

Live DSA Playground

Compact Min-Heap Implementation
Live Result
Console Output

No console output yet. Try calling console.log(...) in JS.

Check Your Understanding

Score: 0 / 1

1. What are the time complexities of peek() and push() on a Binary Min-Heap?

Study Workspace

Finished this lesson?

Your progress is saved locally in your browser, no account needed.

Search courses, lessons, and references

Type to search across all courses, lessons, and quick references.

Press Esc to close Powered by Pagefind