Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Build a Min-Heap

Heapshard

Implement `buildMinHeap(arr)` returning a min-heap array built by sifting down from the last parent to the root (Floyd's build-heap).

Sample tests

Input: buildMinHeap([3,1,2])
Output: [1,3,2]
Input: buildMinHeap([1])
Output: [1]

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Building top-down instead of bottom-up gives an invalid heap.

Learning resources

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

Floyd's bottom-up build-heap runs in O(n), faster than inserting one by one.

Loading...
⌘/Ctrl + Enter

Run your code to see results.