Challenge - 5 Problems
Heap Sort Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Heap Sort on a small array
What is the output of the following JavaScript code that uses heap sort to sort an array?
DSA Javascript
function heapify(arr, n, i) {
let largest = i;
let left = 2 * i + 1;
let right = 2 * i + 2;
if (left < n && arr[left] > arr[largest]) {
largest = left;
}
if (right < n && arr[right] > arr[largest]) {
largest = right;
}
if (largest !== i) {
[arr[i], arr[largest]] = [arr[largest], arr[i]];
heapify(arr, n, largest);
}
}
function heapSort(arr) {
let n = arr.length;
for (let i = Math.floor(n / 2) - 1; i >= 0; i--) {
heapify(arr, n, i);
}
for (let i = n - 1; i > 0; i--) {
[arr[0], arr[i]] = [arr[i], arr[0]];
heapify(arr, i, 0);
}
}
let array = [4, 10, 3, 5, 1];
heapSort(array);
console.log(array.join(' -> ') + ' -> null');Attempts:
2 left
💡 Hint
Heap sort sorts the array in ascending order by building a max heap first.
✗ Incorrect
Heap sort first builds a max heap, then repeatedly swaps the root with the last element and heapifies the reduced heap. The final array is sorted ascending.
🧠 Conceptual
intermediate1:30remaining
Understanding heapify in heap sort
In the heap sort algorithm, what is the main purpose of the heapify function?
Attempts:
2 left
💡 Hint
Heapify fixes the heap property starting from a node downwards.
✗ Incorrect
Heapify ensures that the subtree rooted at a given index is a max heap by comparing the node with its children and swapping if needed.
🔧 Debug
advanced2:00remaining
Identify the error in this heap sort code snippet
What error will this JavaScript code produce when running heap sort?
DSA Javascript
function heapify(arr, n, i) {
let largest = i;
let left = 2 * i + 1;
let right = 2 * i + 2;
if (left < n && arr[left] < arr[largest]) {
largest = left;
}
if (right < n && arr[right] < arr[largest]) {
largest = right;
}
if (largest !== i) {
[arr[i], arr[largest]] = [arr[largest], arr[i]];
heapify(arr, n, largest);
}
}
function heapSort(arr) {
let n = arr.length;
for (let i = Math.floor(n / 2) - 1; i >= 0; i--) {
heapify(arr, n, i);
}
for (let i = n - 1; i > 0; i--) {
[arr[0], arr[i]] = [arr[i], arr[0]];
heapify(arr, i, 0);
}
}
let array = [4, 10, 3, 5, 1];
heapSort(array);
console.log(array.join(' -> ') + ' -> null');Attempts:
2 left
💡 Hint
Check the comparison operators in heapify function.
✗ Incorrect
The heapify function uses '<' instead of '>' which builds a min heap, causing the array to be sorted descending.
❓ Predict Output
advanced2:00remaining
Heap sort output with duplicate elements
What is the output of the following heap sort code when sorting an array with duplicates?
DSA Javascript
function heapify(arr, n, i) {
let largest = i;
let left = 2 * i + 1;
let right = 2 * i + 2;
if (left < n && arr[left] > arr[largest]) {
largest = left;
}
if (right < n && arr[right] > arr[largest]) {
largest = right;
}
if (largest !== i) {
[arr[i], arr[largest]] = [arr[largest], arr[i]];
heapify(arr, n, largest);
}
}
function heapSort(arr) {
let n = arr.length;
for (let i = Math.floor(n / 2) - 1; i >= 0; i--) {
heapify(arr, n, i);
}
for (let i = n - 1; i > 0; i--) {
[arr[0], arr[i]] = [arr[i], arr[0]];
heapify(arr, i, 0);
}
}
let array = [4, 10, 3, 10, 5, 1];
heapSort(array);
console.log(array.join(' -> ') + ' -> null');Attempts:
2 left
💡 Hint
Heap sort handles duplicates by sorting them in ascending order.
✗ Incorrect
Heap sort sorts the array ascending including duplicates, so duplicates appear next to each other in sorted order.
🚀 Application
expert1:30remaining
Heap sort time complexity analysis
What is the time complexity of heap sort in the worst case and why?
Attempts:
2 left
💡 Hint
Consider the cost of building the heap and the cost of extracting elements.
✗ Incorrect
Building the heap is O(n), and each of the n removals requires O(log n) heapify calls, totaling O(n log n).