Skip to content

Commit 6ced353

Browse files
committed
Added hard/max_heap_checker
1 parent 307159c commit 6ced353

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

hard/max_heap_checker.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Using the JavaScript language, have the function maxHeapChecker(arr) take arr
3+
* which represents a heap data structure and determine whether or not it is a
4+
* max heap. A max heap has the property that all nodes in the heap are either
5+
* greater than or equal to each of its children. For example: if arr is
6+
* [100,19,36,17] then this is a max heap and your program should return the
7+
* string max. If the input is not a max heap then your program should return a
8+
* list of nodes in string format, in the order that they appear in the tree,
9+
* that currently do not satisfy the max heap property because the child nodes
10+
* are larger than their parent. For example: if arr is [10,19,52,13,16] then
11+
* your program should return 19,52.
12+
*
13+
* Another example: if arr is [10,19,52,104,14] then your program should return
14+
* 19,52,104
15+
*
16+
* https://www.coderbyte.com/results/bhanson:Max%20Heap%20Checker:JavaScript
17+
*
18+
* @param {array} arr
19+
* @return {string}
20+
*/
21+
function maxHeapChecker(arr) {
22+
// https://en.wikipedia.org/wiki/Binary_heap#Heap_implementation
23+
24+
// Iterative solution: breadth-first search
25+
26+
const invalidNodes = [];
27+
28+
// Initialize to root, each successive queue will have the next level depth of indexes
29+
let queue = [0];
30+
31+
arr.forEach(_ => {
32+
const nextQueue = [];
33+
34+
queue.forEach(nodeIndex => {
35+
const leftIndex = 2 * nodeIndex + 1;
36+
const rightIndex = 2 * nodeIndex + 2;
37+
38+
if (leftIndex < arr.length) {
39+
if (arr[leftIndex] > arr[nodeIndex]) {
40+
invalidNodes.push({
41+
index: leftIndex,
42+
value: arr[leftIndex]
43+
});
44+
}
45+
nextQueue.push(leftIndex);
46+
}
47+
48+
if (rightIndex < arr.length) {
49+
if (arr[rightIndex] > arr[nodeIndex]) {
50+
invalidNodes.push({
51+
index: rightIndex,
52+
value: arr[rightIndex]
53+
});
54+
}
55+
nextQueue.push(rightIndex);
56+
}
57+
});
58+
59+
queue = nextQueue;
60+
});
61+
62+
if (invalidNodes.length === 0) {
63+
return 'max';
64+
}
65+
66+
return invalidNodes.map(node => node.value).join(',');
67+
}
68+
69+
module.exports = maxHeapChecker;

hard/max_heap_checker.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const maxHeapChecker = require('./max_heap_checker');
2+
3+
describe('maxHeapChecker()', () => {
4+
test('passes Coderbyte.com tests', () => {
5+
expect(maxHeapChecker([100, 19, 36, 17])).toBe('max');
6+
7+
expect(maxHeapChecker([10, 19, 52, 13, 16])).toBe('19,52');
8+
9+
expect(maxHeapChecker([10, 19, 52, 104, 14])).toBe('19,52,104');
10+
11+
expect(maxHeapChecker([73, 74, 75, 7, 2, 107])).toBe('74,75,107');
12+
13+
expect(maxHeapChecker([1, 5, 10, 2, 3])).toBe('5,10');
14+
15+
expect(maxHeapChecker([5, 2, 1, 1, 1])).toBe('max');
16+
17+
expect(maxHeapChecker([86, 100, 2])).toBe('100');
18+
19+
expect(maxHeapChecker([73, 74, 81, 79, 90])).toBe('74,81,79,90');
20+
21+
expect(maxHeapChecker([73, 72, 1, 79, 90])).toBe('79,90');
22+
23+
expect(maxHeapChecker([100, 99, 98, 97, 96, 95, 94])).toBe('max');
24+
});
25+
});

0 commit comments

Comments
 (0)