Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Extract Min (sift down)

Heapshard

Implement `heapExtractMin(arr)` returning `[min, newHeap]`: the root, and the heap after moving the last element to the root and sifting it down. For an empty heap return `[null, []]`.

Sample tests

Input: heapExtractMin([1,2,3,4,5])
Output: [1,[2,4,3,5]]
Input: heapExtractMin([5])
Output: [5,[]]

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Forgetting the empty-heap and single-element cases.

Learning resources

  • Wikipedia: Binary heap
Approach & explanation (try first)

Swapping the last element to the root and sifting down restores the heap in O(log n).

Loading...
⌘/Ctrl + Enter

Run your code to see results.