The Coding Vibeswith Dipendra Bhandari

🛠️ Heaps for Interviews

A Heap is a special tree-based data structure that satisfies the heap property:

  • Min Heap: Parent node ≤ its children
  • Max Heap: Parent node ≥ its children

Heaps are commonly used to implement priority queues, scheduling, and for efficient selection problems like finding the k smallest/largest elements.


🌳 Structure and Properties

  • Binary Heap is a complete binary tree (all levels filled left to right).
  • Typically implemented using an array:
    • For index i:
      • left = 2i + 1
      • right = 2i + 2
      • parent = Math.floor((i - 1) / 2)

🔄 Types of Heaps

✅ Min Heap

Smallest element is at the root.

ts// Example: [1, 3, 6, 5, 9, 8]
       1
     /   \
    3     6
   / \   /
  5   9 8