Challenge - 5 Problems
Radix Sort Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Radix Sort on a small array
What is the output of the following Radix Sort implementation on the array [170, 45, 75, 90, 802, 24, 2, 66]?
DSA C
void radixSort(int arr[], int n) { int max = arr[0]; for (int i = 1; i < n; i++) { if (arr[i] > max) max = arr[i]; } for (int exp = 1; max / exp > 0; exp *= 10) { int output[n]; int count[10] = {0}; for (int i = 0; i < n; i++) count[(arr[i] / exp) % 10]++; for (int i = 1; i < 10; i++) count[i] += count[i - 1]; for (int i = n - 1; i >= 0; i--) { output[count[(arr[i] / exp) % 10] - 1] = arr[i]; count[(arr[i] / exp) % 10]--; } for (int i = 0; i < n; i++) arr[i] = output[i]; } } int main() { int arr[] = {170, 45, 75, 90, 802, 24, 2, 66}; int n = sizeof(arr) / sizeof(arr[0]); radixSort(arr, n); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } return 0; }
Attempts:
2 left
💡 Hint
Radix sort sorts numbers digit by digit starting from the least significant digit.
✗ Incorrect
Radix sort processes digits from right to left. After sorting by each digit, the array becomes more sorted. The final sorted array is in ascending order.
🧠 Conceptual
intermediateUnderstanding Radix Sort Stability
Why is it important that the sorting algorithm used at each digit level in Radix Sort is stable?
Attempts:
2 left
💡 Hint
Think about what happens when sorting by multiple digits one after another.
✗ Incorrect
Radix sort sorts digits from least significant to most significant. If the sorting at each digit is stable, the order of elements sorted by previous digits is preserved, ensuring correct final order.
🔧 Debug
advancedIdentify the bug in this Radix Sort code snippet
What error will this Radix Sort code produce when sorting the array [3, 1, 2]?
DSA C
void radixSort(int arr[], int n) { int max = arr[0]; for (int i = 1; i < n; i++) { if (arr[i] > max) max = arr[i]; } for (int exp = 1; max / exp > 0; exp *= 10) { int output[n]; int count[10]; for (int i = 0; i < 10; i++) count[i] = 0; for (int i = 0; i < n; i++) count[(arr[i] / exp) % 10]++; for (int i = 1; i < 10; i++) count[i] += count[i - 1]; for (int i = n - 1; i >= 0; i--) { output[count[(arr[i] / exp) % 10] - 1] = arr[i]; count[(arr[i] / exp) % 10]--; } for (int i = 0; i < n; i++) arr[i] = output[i]; } } int main() { int arr[] = {3, 1, 2}; int n = sizeof(arr) / sizeof(arr[0]); radixSort(arr, n); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } return 0; }
Attempts:
2 left
💡 Hint
Check how the count array is initialized before counting digits.
✗ Incorrect
The count array is initialized with a loop setting all elements to zero before use, so no runtime or syntax error occurs. The output is correctly sorted.
❓ Predict Output
advancedOutput of Radix Sort with numbers having different digit lengths
What is the output of Radix Sort on the array [5, 123, 9, 45, 1000]?
DSA C
void radixSort(int arr[], int n) { int max = arr[0]; for (int i = 1; i < n; i++) { if (arr[i] > max) max = arr[i]; } for (int exp = 1; max / exp > 0; exp *= 10) { int output[n]; int count[10] = {0}; for (int i = 0; i < n; i++) count[(arr[i] / exp) % 10]++; for (int i = 1; i < 10; i++) count[i] += count[i - 1]; for (int i = n - 1; i >= 0; i--) { output[count[(arr[i] / exp) % 10] - 1] = arr[i]; count[(arr[i] / exp) % 10]--; } for (int i = 0; i < n; i++) arr[i] = output[i]; } } int main() { int arr[] = {5, 123, 9, 45, 1000}; int n = sizeof(arr) / sizeof(arr[0]); radixSort(arr, n); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } return 0; }
Attempts:
2 left
💡 Hint
Radix sort sorts numbers by each digit starting from the least significant digit, handling different digit lengths correctly.
✗ Incorrect
Radix sort sorts numbers digit by digit. Numbers with fewer digits are treated as having leading zeros. The final sorted array is ascending.
🧠 Conceptual
expertRadix Sort Time Complexity Analysis
Given an array of n integers where the maximum number has d digits, what is the time complexity of Radix Sort and why?
Attempts:
2 left
💡 Hint
Consider how many passes and what sorting method is used at each digit.
✗ Incorrect
Radix sort performs d passes over the array, each pass using counting sort which runs in O(n + k) time, where k is the digit range (usually 10). So total time is O(d * (n + k)).
