Common Mistakes

Common errors to avoid

  • Wrong heap type: Max-heap for k smallest, min-heap for k largest seems backward but is correct for maintaining k elements.
  • Forgetting heap property after modification: Don't modify heap elements directly.

Extract, modify, reinsert to maintain heap property.

  • Not handling equal elements: When multiple elements have same priority, ensure consistent handling (FIFO or by secondary key).
  • Heap size off by one: When maintaining size k, check boundary conditions for when heap has fewer than k elements.