Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to insert a number into the max heap.
DSA C
void insertMaxHeap(int heap[], int *size, int value) {
heap[++(*size)] = [1];
int i = *size;
while (i > 1 && heap[i / 2] < heap[i]) {
int temp = heap[i];
heap[i] = heap[i / 2];
heap[i / 2] = temp;
i /= 2;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong variable instead of 'value' to insert.
Not incrementing the size before insertion.
✗ Incorrect
We insert the new value at the end of the heap array before heapifying up.
2fill in blank
mediumComplete the code to balance the sizes of the two heaps.
DSA C
void balanceHeaps(int maxHeap[], int *maxSize, int minHeap[], int *minSize) {
if (*maxSize > *minSize + 1) {
int value = extractMax(maxHeap, maxSize);
insertMinHeap(minHeap, minSize, [1]);
} else if (*minSize > *maxSize) {
int value = extractMin(minHeap, minSize);
insertMaxHeap(maxHeap, maxSize, value);
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Inserting the wrong variable into minHeap.
Using heap size variables instead of the extracted value.
✗ Incorrect
The extracted max value from maxHeap should be inserted into minHeap to balance sizes.
3fill in blank
hardFix the error in the median calculation code.
DSA C
double getMedian(int maxHeap[], int maxSize, int minHeap[], int minSize) {
if (maxSize == minSize) {
return (maxHeap[1] + [1]) / 2.0;
} else {
return maxHeap[1];
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using index 0 for heap root which is incorrect in 1-based heap arrays.
Using maxHeap elements instead of minHeap for the second median value.
✗ Incorrect
Median is average of max of maxHeap and min of minHeap, which are at index 1 in heaps.
4fill in blank
hardFill both blanks to correctly insert a new number into the appropriate heap.
DSA C
void addNumber(int num, int maxHeap[], int *maxSize, int minHeap[], int *minSize) {
if (*maxSize == 0 || num <= maxHeap[1]) {
insertMaxHeap(maxHeap, maxSize, [1]);
} else {
insertMinHeap(minHeap, minSize, [2]);
}
balanceHeaps(maxHeap, maxSize, minHeap, minSize);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using heap sizes instead of the number to insert.
Inserting wrong heap root values instead of 'num'.
✗ Incorrect
The new number 'num' is inserted into maxHeap or minHeap based on comparison.
5fill in blank
hardFill both blanks to correctly extract the max from maxHeap.
DSA C
int extractMax(int heap[], int *size) {
int max = heap[1];
heap[1] = heap[[1]];
(*size)--;
int i = 1;
while (i * 2 <= *size) {
int left = i * 2;
int right = i * 2 + 1;
int largest = i;
if (heap[left] > heap[largest]) largest = left;
if (right <= *size && heap[right] > heap[largest]) largest = right;
if (largest == i) break;
int temp = heap[i];
heap[i] = heap[largest];
heap[largest] = temp;
i = largest;
}
return [2];
}Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong index to replace root.
Returning wrong variable instead of max.
✗ Incorrect
Replace root with last element at index *size, decrease size, then return max.
