Challenge - 5 Problems
Radix Sort Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output 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++
void countingSort(int arr[], int n, int exp) { 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]; } void radixSort(int arr[], int n) { int maxVal = arr[0]; for (int i = 1; i < n; i++) if (arr[i] > maxVal) maxVal = arr[i]; for (int exp = 1; maxVal / exp > 0; exp *= 10) countingSort(arr, n, exp); } 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++) std::cout << arr[i] << " "; return 0; }
Attempts:
2 left
💡 Hint
Radix sort sorts numbers digit by digit starting from least significant digit.
✗ Incorrect
Radix sort uses counting sort on each digit starting from the least significant digit. After sorting all digits, the array becomes sorted in ascending order.
❓ Predict Output
intermediate2:00remaining
Radix Sort output with numbers having different digit lengths
What will be the output of the following code after applying Radix Sort on the array?
DSA C++
int arr[] = {3, 300, 34, 5, 9}; int n = sizeof(arr) / sizeof(arr[0]); radixSort(arr, n); for (int i = 0; i < n; i++) std::cout << arr[i] << " ";
Attempts:
2 left
💡 Hint
Radix sort handles numbers with different digit lengths by sorting from least significant digit.
✗ Incorrect
Radix sort sorts numbers digit by digit. Smaller numbers with fewer digits come before larger numbers when sorted correctly.
🧠 Conceptual
advanced1:30remaining
Why Radix Sort is not comparison-based?
Which statement best explains why Radix Sort is not a comparison-based sorting algorithm?
Attempts:
2 left
💡 Hint
Think about how Radix Sort processes digits instead of comparing whole numbers.
✗ Incorrect
Radix Sort processes digits of numbers and uses counting sort on each digit, which does not involve comparing elements directly.
🔧 Debug
advanced2:00remaining
Identify the bug in this Radix Sort counting sort implementation
What error will this code cause when running countingSort in Radix Sort?
DSA C++
void countingSort(int arr[], int n, int exp) { 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]; }
Attempts:
2 left
💡 Hint
Check the loop that updates the count array prefix sums.
✗ Incorrect
The loop runs from i=1 to i<=10, but count array has size 10 (indices 0 to 9). Accessing count[10] causes out of bounds error.
🚀 Application
expert2:30remaining
Radix Sort on strings of digits
Given an array of strings representing numbers with equal length digits, which approach correctly sorts them using Radix Sort?
Attempts:
2 left
💡 Hint
Radix Sort processes digits starting from least significant digit.
✗ Incorrect
Radix Sort on strings representing numbers sorts from rightmost character (least significant digit) to leftmost using counting sort at each step.