0
0
DSA C++programming~20 mins

Radix Sort Algorithm in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Radix Sort Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
A802 170 90 75 66 45 24 2
B2 24 45 66 75 90 170 802
C2 24 45 66 75 170 90 802
D170 45 75 90 802 24 2 66
Attempts:
2 left
💡 Hint
Radix sort sorts numbers digit by digit starting from least significant digit.
Predict Output
intermediate
2: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] << " ";
A3 5 9 34 300
B300 34 9 5 3
C3 34 5 9 300
D5 3 9 34 300
Attempts:
2 left
💡 Hint
Radix sort handles numbers with different digit lengths by sorting from least significant digit.
🧠 Conceptual
advanced
1:30remaining
Why Radix Sort is not comparison-based?
Which statement best explains why Radix Sort is not a comparison-based sorting algorithm?
AIt sorts numbers by grouping digits and using counting sort without direct element comparisons.
BIt sorts numbers by comparing each pair of elements multiple times.
CIt uses quicksort internally to sort digits of numbers.
DIt compares elements only once and then sorts them using a heap.
Attempts:
2 left
💡 Hint
Think about how Radix Sort processes digits instead of comparing whole numbers.
🔧 Debug
advanced
2: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];
}
ALogical error causing reversed sorted array
BNo error, code runs correctly
CCompilation error due to missing semicolon
DArray index out of bounds error due to incorrect loop limit in count array update
Attempts:
2 left
💡 Hint
Check the loop that updates the count array prefix sums.
🚀 Application
expert
2: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?
ASort strings from leftmost character to rightmost using counting sort on each character
BSort strings lexicographically using built-in sort only
CSort strings from rightmost character to leftmost using counting sort on each character
DConvert strings to integers and apply quicksort
Attempts:
2 left
💡 Hint
Radix Sort processes digits starting from least significant digit.