Challenge - 5 Problems
Kth Smallest Heap Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Kth Smallest Element Extraction
What is the output of the following C code that finds the 3rd smallest element using a min heap?
DSA C
#include <stdio.h> #include <stdlib.h> void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } void minHeapify(int heap[], int size, int i) { int smallest = i; int left = 2 * i + 1; int right = 2 * i + 2; if (left < size && heap[left] < heap[smallest]) smallest = left; if (right < size && heap[right] < heap[smallest]) smallest = right; if (smallest != i) { swap(&heap[i], &heap[smallest]); minHeapify(heap, size, smallest); } } int extractMin(int heap[], int *size) { if (*size <= 0) return -1; int root = heap[0]; heap[0] = heap[*size - 1]; (*size)--; minHeapify(heap, *size, 0); return root; } int kthSmallest(int arr[], int n, int k) { int *heap = (int *)malloc(n * sizeof(int)); for (int i = 0; i < n; i++) heap[i] = arr[i]; for (int i = n / 2 - 1; i >= 0; i--) minHeapify(heap, n, i); int size = n; int result = -1; for (int i = 0; i < k; i++) result = extractMin(heap, &size); free(heap); return result; } int main() { int arr[] = {7, 10, 4, 3, 20, 15}; int n = sizeof(arr) / sizeof(arr[0]); int k = 3; int res = kthSmallest(arr, n, k); printf("%d\n", res); return 0; }
Attempts:
2 left
💡 Hint
Remember the min heap always extracts the smallest element first.
✗ Incorrect
The min heap extracts elements in ascending order. The 1st smallest is 3, 2nd is 4, and 3rd is 7.
❓ Predict Output
intermediateResulting Heap After Building Min Heap
What is the state of the heap array after building the min heap from this array: [12, 3, 5, 7, 19]?
DSA C
#include <stdio.h> void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } void minHeapify(int heap[], int size, int i) { int smallest = i; int left = 2 * i + 1; int right = 2 * i + 2; if (left < size && heap[left] < heap[smallest]) smallest = left; if (right < size && heap[right] < heap[smallest]) smallest = right; if (smallest != i) { swap(&heap[i], &heap[smallest]); minHeapify(heap, size, smallest); } } int main() { int heap[] = {12, 3, 5, 7, 19}; int size = 5; for (int i = size / 2 - 1; i >= 0; i--) { minHeapify(heap, size, i); } for (int i = 0; i < size; i++) { printf("%d ", heap[i]); } return 0; }
Attempts:
2 left
💡 Hint
Start heapifying from the last non-leaf node upwards.
✗ Incorrect
After heapify, the smallest element is at root (index 0). The array represents the min heap in level order.
🧠 Conceptual
advancedUnderstanding Min Heap Extraction
When extracting the minimum element from a min heap, which step is NOT performed?
Attempts:
2 left
💡 Hint
Think about what heapify does versus sorting.
✗ Incorrect
Extracting min involves replacing root with last element, reducing size, and heapifying root. Sorting entire heap is not done.
❓ Predict Output
advancedOutput After Multiple ExtractMin Calls
Given the min heap array [2, 4, 3, 7, 9, 10], what is the output after two extractMin operations?
DSA C
#include <stdio.h> void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } void minHeapify(int heap[], int size, int i) { int smallest = i; int left = 2 * i + 1; int right = 2 * i + 2; if (left < size && heap[left] < heap[smallest]) smallest = left; if (right < size && heap[right] < heap[smallest]) smallest = right; if (smallest != i) { swap(&heap[i], &heap[smallest]); minHeapify(heap, size, smallest); } } int extractMin(int heap[], int *size) { if (*size <= 0) return -1; int root = heap[0]; heap[0] = heap[*size - 1]; (*size)--; minHeapify(heap, *size, 0); return root; } int main() { int heap[] = {2, 4, 3, 7, 9, 10}; int size = 6; int first = extractMin(heap, &size); int second = extractMin(heap, &size); printf("%d %d\n", first, second); return 0; }
Attempts:
2 left
💡 Hint
ExtractMin returns the root each time and then heapifies.
✗ Incorrect
First extractMin returns 2, then heapify makes 3 the root. Second extractMin returns 3, but after heapify 4 becomes root. So second extracted is 4.
🧠 Conceptual
expertTime Complexity of Kth Smallest Using Min Heap
What is the time complexity of finding the kth smallest element in an array of size n using a min heap built from the entire array?
Attempts:
2 left
💡 Hint
Consider heap building and k extractions separately.
✗ Incorrect
Building a min heap takes O(n). Extracting min k times takes O(k log n). Total is O(n + k log n).
