diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/insert-an-element-into-a-max-heap.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/insert-an-element-into-a-max-heap.english.md index 06cbc5268e..9214e34aff 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/insert-an-element-into-a-max-heap.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/insert-an-element-into-a-max-heap.english.md @@ -68,6 +68,24 @@ var MaxHeap = function() {
```js -// solution required +var MaxHeap = function() { + // change code below this line + this.heap = [undefined]; + this.insert = (ele) => { + var index = this.heap.length; + var arr = [...this.heap]; + arr.push(ele); + while (ele > arr[Math.floor(index / 2)]) { + 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 + } + // change code above this line +}; ```