Skip to main content

Heaps

This technique uses Min-Heap (or Max-Heap) whenever you need quick access to the smallest (or largest) item in input data.

A Min-Heap is a complete binary tree (that is, totally filled other than the rightmost elements on the last level) where each node is smaller than its children. The root, therefore, is the minimum element in the tree.

A Max-Heap is essentially equivalent. But each node is larger than its children. The root, therefore, is the maximum element in the tree.

How to Identify

  • Data Structure Involves: Array, Heap, Queue
  • Question Type: Find the smallest/largest/median/frequent ‘K’ elements of a set
ProblemsDifficulty
1K Closest Points to OriginMedium
2Top K Frequent ElementsMedium
3Kth Smallest Element in a Sorted MatrixMedium
4Kth Largest Element in an ArrayMedium
5Merge K Sorted ListsHard
6Find Median from Data StreamHard