Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to insert an element into a max heap array.
DSA Java
public void insert(int[] heap, int value, int size) {
heap[size] = value;
int current = size;
while (current > 0 && heap[current] [1] heap[(current - 1) / 2]) {
int temp = heap[current];
heap[current] = heap[(current - 1) / 2];
heap[(current - 1) / 2] = temp;
current = (current - 1) / 2;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' causes the heap to become a min heap.
Using '==' does not maintain heap property.
Not swapping when necessary.
✗ Incorrect
In a max heap, the inserted element is compared with its parent, and if it is greater, they are swapped to maintain the max heap property.
2fill in blank
mediumComplete the code to heapify a subtree rooted at index i in a max heap.
DSA Java
public void heapify(int[] heap, int size, int i) {
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < size && heap[left] [1] heap[largest]) {
largest = left;
}
if (right < size && heap[right] [1] heap[largest]) {
largest = right;
}
if (largest != i) {
int swap = heap[i];
heap[i] = heap[largest];
heap[largest] = swap;
heapify(heap, size, largest);
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' causes min heap behavior.
Using '==' does not help find the largest.
Not checking array bounds before accessing children.
✗ Incorrect
To maintain max heap property, we find the largest among root, left child, and right child. We compare children with '>' to find the largest.
3fill in blank
hardFix the error in the code that extracts the maximum element from a max heap.
DSA Java
public int extractMax(int[] heap, int size) {
if (size <= 0) return -1;
int max = heap[0];
heap[0] = heap[size - 1];
size = size - 1;
heapify(heap, size, [1]);
return max;
}Attempts:
3 left
💡 Hint
Common Mistakes
Starting heapify at wrong index causes incorrect heap structure.
Using size or size-1 as index causes out-of-bound errors.
✗ Incorrect
After replacing the root with the last element, heapify should start at index 0 to restore max heap property.
4fill in blank
hardFill both blanks to build a max heap from an unsorted array.
DSA Java
public void buildMaxHeap(int[] heap, int size) {
for (int i = [1]; i >= 0; i--) {
heapify(heap, size, [2]);
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Starting from size - 1 causes unnecessary heapify calls on leaf nodes.
Using 0 as start index causes no heap building.
✗ Incorrect
Building a max heap starts from the last non-leaf node at index size/2 - 1 down to 0, calling heapify on each.
5fill in blank
hardFill all three blanks to implement heap sort using a max heap.
DSA Java
public void heapSort(int[] arr) {
int n = arr.length;
buildMaxHeap(arr, n);
for (int i = n - 1; i > 0; i--) {
int temp = arr[[1]];
arr[[2]] = arr[0];
arr[0] = temp;
heapify(arr, [3], 0);
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Swapping wrong indices causes incorrect sorting.
Heapifying with wrong size or index breaks heap property.
✗ Incorrect
In heap sort, swap the root with the last element (index i), then heapify the reduced heap of size i starting at root 0.
