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 list
What is the output of the following Python code that uses Radix Sort to sort the list?
DSA Python
def counting_sort(arr, exp): n = len(arr) output = [0] * n count = [0] * 10 for i in range(n): index = (arr[i] // exp) % 10 count[index] += 1 for i in range(1, 10): count[i] += count[i - 1] i = n - 1 while i >= 0: index = (arr[i] // exp) % 10 output[count[index] - 1] = arr[i] count[index] -= 1 i -= 1 for i in range(n): arr[i] = output[i] def radix_sort(arr): max1 = max(arr) exp = 1 while max1 // exp > 0: counting_sort(arr, exp) exp *= 10 arr = [170, 45, 75, 90, 802, 24, 2, 66] radix_sort(arr) print(arr)
Attempts:
2 left
💡 Hint
Think about how Radix Sort sorts numbers digit by digit from least significant to most significant.
✗ Incorrect
Radix Sort sorts numbers by processing each digit starting from the least significant digit to the most significant digit using a stable counting sort. The final sorted list is in ascending order.
🧠 Conceptual
intermediateNumber of passes in Radix Sort
If you apply Radix Sort to sort a list of integers where the largest number has 4 digits, how many passes (iterations over the list) will the algorithm perform?
Attempts:
2 left
💡 Hint
Radix Sort processes digits one by one from least significant to most significant.
✗ Incorrect
Radix Sort performs one pass for each digit place. For a 4-digit number, it will perform 4 passes.
🔧 Debug
advancedIdentify the error in this Radix Sort implementation
What error will the following code produce when run?
DSA Python
def counting_sort(arr, exp): n = len(arr) output = [0] * n count = [0] * 10 for i in range(n): index = (arr[i] // exp) % 10 count[index] += 1 for i in range(1, 10): count[i] += count[i - 1] i = 0 while i >= 0: index = (arr[i] // exp) % 10 output[count[index] - 1] = arr[i] count[index] -= 1 i -= 1 for i in range(n): arr[i] = output[i] arr = [170, 45, 75] counting_sort(arr, 1) print(arr)
Attempts:
2 left
💡 Hint
Look at the while loop condition and how the index variable changes.
✗ Incorrect
The while loop starts with i=0 and decrements i each iteration while i >= 0, so it never stops and causes an infinite loop.
🚀 Application
advancedRadix Sort on strings of digits
Given a list of strings representing numbers with equal length, which option shows the correct sorted list after applying Radix Sort (digit by digit from right to left)?
DSA Python
arr = ['170', '045', '075', '090', '802', '024', '002', '066'] # Radix Sort applied treating strings as numbers digit-wise
Attempts:
2 left
💡 Hint
Radix Sort sorts digit by digit from right to left, preserving order for equal digits.
✗ Incorrect
Sorting strings digit-wise from right to left results in ascending numeric order with leading zeros preserved.
🧠 Conceptual
expertTime complexity of Radix Sort
What is the time complexity of Radix Sort when sorting n numbers each having d digits, assuming counting sort is used as the stable sort for each digit?
Attempts:
2 left
💡 Hint
Consider that counting sort runs in O(n + k) and is applied d times.
✗ Incorrect
Radix Sort runs counting sort d times, each taking O(n + k), so total is O(d * (n + k)). Usually k=10 for digits.
