diff --git a/curriculum/challenges/english/10-coding-interview-prep/data-structures/insert-an-element-into-a-max-heap.english.md b/curriculum/challenges/english/10-coding-interview-prep/data-structures/insert-an-element-into-a-max-heap.english.md index 184bf7939e..f4a70a4791 100644 --- a/curriculum/challenges/english/10-coding-interview-prep/data-structures/insert-an-element-into-a-max-heap.english.md +++ b/curriculum/challenges/english/10-coding-interview-prep/data-structures/insert-an-element-into-a-max-heap.english.md @@ -70,12 +70,12 @@ var MaxHeap = function() { ```js var MaxHeap = function() { // change code below this line - this.heap = [undefined]; + this.heap = [null]; this.insert = (ele) => { var index = this.heap.length; var arr = [...this.heap]; arr.push(ele); - while (ele > arr[Math.floor(index / 2)]) { + while (ele > arr[Math.floor(index / 2)] && index > 1) { arr[index] = arr[Math.floor(index / 2)]; arr[Math.floor(index / 2)] = ele; index = arr[Math.floor(index / 2)]; @@ -83,7 +83,7 @@ var MaxHeap = function() { this.heap = arr; } this.print = () => { - return this.heap + return this.heap.slice(1); } // change code above this line }; diff --git a/curriculum/challenges/english/10-coding-interview-prep/data-structures/remove-an-element-from-a-max-heap.english.md b/curriculum/challenges/english/10-coding-interview-prep/data-structures/remove-an-element-from-a-max-heap.english.md index 86376e2ef3..6a66fd0cbd 100644 --- a/curriculum/challenges/english/10-coding-interview-prep/data-structures/remove-an-element-from-a-max-heap.english.md +++ b/curriculum/challenges/english/10-coding-interview-prep/data-structures/remove-an-element-from-a-max-heap.english.md @@ -18,7 +18,7 @@ Now that we can add elements to our heap let's see how we can remove elements. R ## Instructions
-Instructions: Add a method to our max heap called remove. This method should return the greatest value that has been added to our max heap and remove it from the heap. It should also reorder the heap so the heap property is maintained. After removing an element, the next greatest element remaining in the heap should become the root. Add your insert method again here as well. +Instructions: Add a method to our max heap called remove. This method should return the greatest value that has been added to our max heap and remove it from the heap. It should also reorder the heap so the heap property is maintained. After removing an element, the next greatest element remaining in the heap should become the root.
## Tests @@ -47,7 +47,23 @@ tests: ```js var MaxHeap = function() { + this.heap = [null]; + this.insert = (ele) => { + var index = this.heap.length; + var arr = [...this.heap]; + arr.push(ele); + while (ele > arr[Math.floor(index / 2)] && index > 1) { + arr[index] = arr[Math.floor(index / 2)]; + arr[Math.floor(index / 2)] = ele; + index = arr[Math.floor(index / 2)]; + } + this.heap = arr; + } + this.print = () => { + return this.heap.slice(1); + } // change code below this line + // change code above this line }; ```