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 C# code after applying Radix Sort on the array?
DSA C#
using System; class Program { static void RadixSort(int[] arr) { int max = arr[0]; foreach (int num in arr) { if (num > max) max = num; } for (int exp = 1; max / exp > 0; exp *= 10) { CountSort(arr, exp); } } static void CountSort(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 index = (arr[i] / exp) % 10; output[count[index] - 1] = arr[i]; count[index]--; } for (int i = 0; i < n; i++) { arr[i] = output[i]; } } static void Main() { int[] arr = {170, 45, 75, 90, 802, 24, 2, 66}; RadixSort(arr); foreach (int num in arr) { Console.Write(num + " "); } } }
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 ascending: 2 24 45 66 75 90 170 802.
🧠 Conceptual
intermediateUnderstanding Radix Sort Stability
Why is stability important in Radix Sort when sorting numbers digit by digit?
Attempts:
2 left
💡 Hint
Think about what happens when sorting by each digit and how previous order affects the final result.
✗ Incorrect
Radix sort sorts digits from least significant to most significant. If sorting by a digit is unstable, the order of numbers with the same digit can change, breaking the correct final order. Stability preserves order of equal elements, so the final sorted array is correct.
🔧 Debug
advancedIdentify the error in this Radix Sort implementation
What error will this code produce when running Radix Sort on the array?
DSA C#
void CountSort(int[] arr, int exp) { int n = arr.Length; int[] output = new int[n]; int[] count = new int[9]; for (int i = 0; i < n; i++) { count[(arr[i] / exp) % 10]++; } for (int i = 1; i < 9; i++) { count[i] += count[i - 1]; } for (int i = n - 1; i >= 0; i--) { int index = (arr[i] / exp) % 10; output[count[index] - 1] = arr[i]; count[index]--; } for (int i = 0; i < n; i++) { arr[i] = output[i]; } }
Attempts:
2 left
💡 Hint
Check the size of the count array and the indexing used.
✗ Incorrect
The count array is initialized with size 9 but digits range from 0 to 9, so index 9 access causes IndexOutOfRangeException.
🚀 Application
advancedRadix Sort on Strings of Equal Length
Given an array of strings all of length 3 containing only lowercase letters, which approach correctly sorts them lexicographically using Radix Sort?
Attempts:
2 left
💡 Hint
Radix sort processes digits/characters from least significant to most significant with stable sorting.
✗ Incorrect
Radix sort for strings works by sorting characters from right to left using a stable sort. This preserves order of previous characters and results in lexicographical order.
🧠 Conceptual
expertTime Complexity of Radix Sort with Different Bases
If Radix Sort is implemented with base b (digits 0 to b-1) to sort n numbers with d digits each, what is the time complexity?
Attempts:
2 left
💡 Hint
Consider that each digit is sorted using counting sort which takes O(n + b) time.
✗ Incorrect
Radix sort runs counting sort d times. Counting sort takes O(n + b) time. So total is O(d * (n + b)).
