Some problems keep asking the same question: which item is the smallest right now? A job runner wants the task with the earliest deadline, a dashboard wants the slowest responses so far, and a route planner wants the closest place it hasn’t visited yet. New items keep arriving, so sorting once doesn’t help, and scanning the whole array every time costs O(n) per question.
A heap answers that question in O(1) and takes an item in or out in O(log n). Many languages ship a priority queue in their standard library. JavaScript doesn’t, so in an interview you either write a small one or ask whether you may assume it exists. In production code, a library such as heap-js does the job.
A Tree Hiding in an Array
A min-heap is a binary tree with one rule: every parent is less than or equal to its children. So the smallest item always sits at the root. Nothing else is sorted, which is why the heap stays cheap to maintain.
The tree is stored in an ordinary array, level by level. The root is at index 0, the children of index i are at 2 * i + 1 and 2 * i + 2, and the parent of i is at (i - 1) >> 1. No node objects, no references, just arithmetic.