Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Is a Min-Heap

Heapsmedium

An array represents a binary heap where node `i` has children `2i+1` and `2i+2`. Implement `isMinHeap(arr)` returning whether every parent is less than or equal to its children.

Sample tests

Input: isMinHeap([1,2,3,4,5])
Output: true
Input: isMinHeap([3,1,2])
Output: false

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Reading past the array bounds for missing children.

Learning resources

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

Checking the parent-child relationship for every index validates the heap in O(n).

Loading...
⌘/Ctrl + Enter

Run your code to see results.