Challenge - 5 Problems
Radix Sort Mastery
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 Java code after applying Radix Sort on the array?
DSA Java
import java.util.*; public class RadixSortExample { public static void radixSort(int[] arr) { int max = Arrays.stream(arr).max().getAsInt(); for (int exp = 1; max / exp > 0; exp *= 10) { countingSortByDigit(arr, exp); } } private static void countingSortByDigit(int[] arr, int exp) { int n = arr.length; int[] output = new int[n]; int[] count = new int[10]; 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--) { int digit = (arr[i] / exp) % 10; output[count[digit] - 1] = arr[i]; count[digit]--; } System.arraycopy(output, 0, arr, 0, n); } public static void main(String[] args) { int[] arr = {170, 45, 75, 90, 802, 24, 2, 66}; radixSort(arr); System.out.print(Arrays.toString(arr)); } }
Attempts:
2 left
💡 Hint
Radix sort sorts numbers digit by digit starting from the least significant digit.
✗ Incorrect
Radix sort processes digits from least significant to most significant using counting sort as a subroutine. The final sorted array is in ascending order.
🧠 Conceptual
intermediateUnderstanding Radix Sort Stability
Why is stability important in Radix Sort?
Attempts:
2 left
💡 Hint
Think about how sorting digit by digit works and why order preservation matters.
✗ Incorrect
Radix Sort sorts digits starting from the least significant digit. Stability ensures that when sorting by a more significant digit, the order of numbers with the same digit remains as sorted by previous digits.
🔧 Debug
advancedIdentify the bug in this Radix Sort implementation
What error will this Java code produce when run?
DSA Java
public class RadixSortBug { public static void radixSort(int[] arr) { int max = arr[0]; for (int i = 1; i < arr.length; i++) { if (arr[i] > max) max = arr[i]; } for (int exp = 1; max / exp > 0; exp *= 10) { countingSortByDigit(arr, exp); } } private static void countingSortByDigit(int[] arr, int exp) { // Implementation omitted for brevity } }
Attempts:
2 left
💡 Hint
Check the loop condition carefully when iterating over array indices.
✗ Incorrect
The loop uses i <= arr.length which tries to access arr[arr.length], causing ArrayIndexOutOfBoundsException.
❓ Predict Output
advancedRadix Sort output with numbers having different digit lengths
What is the output of this Java code after applying Radix Sort on the array?
DSA Java
import java.util.*; public class RadixSortTest { public static void radixSort(int[] arr) { int max = Arrays.stream(arr).max().getAsInt(); for (int exp = 1; max / exp > 0; exp *= 10) { countingSortByDigit(arr, exp); } } private static void countingSortByDigit(int[] arr, int exp) { int n = arr.length; int[] output = new int[n]; int[] count = new int[10]; 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--) { int digit = (arr[i] / exp) % 10; output[count[digit] - 1] = arr[i]; count[digit]--; } System.arraycopy(output, 0, arr, 0, n); } public static void main(String[] args) { int[] arr = {5, 123, 9, 45, 1000, 56}; radixSort(arr); System.out.print(Arrays.toString(arr)); } }
Attempts:
2 left
💡 Hint
Radix sort handles numbers with different digit lengths by treating missing digits as zero.
✗ Incorrect
Radix sort sorts numbers digit by digit from least significant to most significant. The final sorted array is ascending.
🧠 Conceptual
expertRadix Sort Time Complexity Analysis
What is the time complexity of Radix Sort for sorting n numbers with d digits each, assuming counting sort is used as the stable sorting subroutine?
Attempts:
2 left
💡 Hint
Counting sort runs in O(n + k) and is applied d times.
✗ Incorrect
Radix sort applies counting sort d times, each taking O(n + k) time. Since k (digit range) is constant (10), complexity is O(d * n).
