Challenge - 5 Problems
Counting Sort Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Counting Sort on a small array
What is the output of the following counting sort implementation on the array [4, 2, 2, 8, 3, 3, 1]?
DSA C
void countingSort(int arr[], int n) { int output[n]; int max = arr[0]; for (int i = 1; i < n; i++) { if (arr[i] > max) max = arr[i]; } int count[max + 1]; for (int i = 0; i <= max; i++) count[i] = 0; for (int i = 0; i < n; i++) count[arr[i]]++; for (int i = 1; i <= max; i++) count[i] += count[i - 1]; for (int i = n - 1; i >= 0; i--) { output[count[arr[i]] - 1] = arr[i]; count[arr[i]]--; } for (int i = 0; i < n; i++) arr[i] = output[i]; } int main() { int arr[] = {4, 2, 2, 8, 3, 3, 1}; int n = sizeof(arr) / sizeof(arr[0]); countingSort(arr, n); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } return 0; }
Attempts:
2 left
💡 Hint
Counting sort sorts numbers by counting occurrences and placing them in order.
✗ Incorrect
Counting sort counts how many times each number appears, then places them in sorted order. The output is the sorted array.
🧠 Conceptual
intermediateCounting Sort Stability Property
Which statement best describes the stability property of counting sort?
Attempts:
2 left
💡 Hint
Think about whether equal numbers keep their original order after sorting.
✗ Incorrect
Counting sort is stable because it places elements in output array in a way that preserves their original order when equal.
❓ Predict Output
advancedCounting Sort with Negative Numbers
What will happen if the following counting sort code is run on the array [-2, -5, -3, 0, -1]?
DSA C
void countingSort(int arr[], int n) { int max = arr[0]; for (int i = 1; i < n; i++) { if (arr[i] > max) max = arr[i]; } int count[max + 1]; for (int i = 0; i <= max; i++) count[i] = 0; for (int i = 0; i < n; i++) count[arr[i]]++; for (int i = 1; i <= max; i++) count[i] += count[i - 1]; int output[n]; for (int i = n - 1; i >= 0; i--) { output[count[arr[i]] - 1] = arr[i]; count[arr[i]]--; } for (int i = 0; i < n; i++) arr[i] = output[i]; }
Attempts:
2 left
💡 Hint
Array indices cannot be negative in C.
✗ Incorrect
Counting sort as implemented uses array indices equal to values. Negative numbers cause invalid negative indices, leading to runtime error.
🔧 Debug
advancedIdentify the bug in counting sort code
What is the bug in this counting sort code snippet?
DSA C
void countingSort(int arr[], int n) { int max = arr[0]; for (int i = 1; i < n; i++) { if (arr[i] > max) max = arr[i]; } int count[max]; for (int i = 0; i < max; i++) count[i] = 0; for (int i = 0; i < n; i++) count[arr[i]]++; for (int i = 1; i < max; i++) count[i] += count[i - 1]; int output[n]; for (int i = n - 1; i >= 0; i--) { output[count[arr[i]] - 1] = arr[i]; count[arr[i]]--; } for (int i = 0; i < n; i++) arr[i] = output[i]; }
Attempts:
2 left
💡 Hint
Check the size of the count array and the maximum index accessed.
✗ Incorrect
The count array must have size max + 1 to include the max value index. Using max size causes out-of-bounds access.
🚀 Application
expertCounting Sort Time Complexity Analysis
Given an input array of size n with values ranging from 0 to k, what is the time complexity of counting sort?
Attempts:
2 left
💡 Hint
Counting sort counts occurrences and then outputs sorted array.
✗ Incorrect
Counting sort runs in O(n + k) because it counts frequencies (k) and processes n elements.
