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 Rust code that implements Radix Sort on the array [170, 45, 75, 90, 802, 24, 2, 66]?
DSA Rust
fn counting_sort(arr: &mut [u32], exp: u32) {
let n = arr.len();
let mut output = vec![0u32; n];
let mut count = vec![0usize; 10];
for &num in arr.iter() {
let index = (num / exp) % 10;
count[index as usize] += 1;
}
for i in 1..10 {
count[i] += count[i - 1];
}
for &num in arr.iter().rev() {
let index = (num / exp) % 10;
output[count[index as usize] - 1] = num;
count[index as usize] -= 1;
}
for i in 0..n {
arr[i] = output[i];
}
}
fn radix_sort(arr: &mut [u32]) {
let max = *arr.iter().max().unwrap();
let mut exp = 1;
while max / exp > 0 {
counting_sort(arr, exp);
exp *= 10;
}
}
fn main() {
let mut arr = vec![170, 45, 75, 90, 802, 24, 2, 66];
radix_sort(&mut arr);
println!("{:?}", 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
intermediateNumber of passes in Radix Sort
Given an array of unsigned integers where the maximum number is 9999, how many passes (iterations over digits) will the Radix Sort algorithm perform?
Attempts:
2 left
💡 Hint
Count the number of digits in the maximum number.
✗ Incorrect
Radix sort performs one pass per digit. Since 9999 has 4 digits, it will perform 4 passes.
🔧 Debug
advancedIdentify the bug causing incorrect sorting
The following Rust code attempts to implement Radix Sort but produces incorrect output. What is the bug?
DSA Rust
fn counting_sort(arr: &mut [u32], exp: u32) {
let n = arr.len();
let mut output = vec![0u32; n];
let mut count = vec![0usize; 10];
for &num in arr.iter() {
let index = (num % exp) / 10;
count[index as usize] += 1;
}
for i in 1..10 {
count[i] += count[i - 1];
}
for &num in arr.iter().rev() {
let index = (num % exp) / 10;
output[count[index as usize] - 1] = num;
count[index as usize] -= 1;
}
for i in 0..n {
arr[i] = output[i];
}
}
fn radix_sort(arr: &mut [u32]) {
let max = *arr.iter().max().unwrap();
let mut exp = 1;
while max / exp > 0 {
counting_sort(arr, exp);
exp *= 10;
}
}Attempts:
2 left
💡 Hint
Check how digits are extracted for sorting.
✗ Incorrect
The expression (num % exp) / 10 does not correctly extract the digit at the current exponent. The correct expression is (num / exp) % 10.
🚀 Application
advancedRadix Sort on strings of digits
Which option correctly describes how Radix Sort can be adapted to sort an array of strings representing numbers with equal length, e.g. ["170", "045", "075", "090"]?
Attempts:
2 left
💡 Hint
Radix Sort processes digits from least significant to most significant.
✗ Incorrect
Radix Sort can sort fixed-length strings by sorting characters from right to left using counting sort at each position.
🧠 Conceptual
expertTime complexity of Radix Sort with base 10
Given n numbers each with d digits, what is the time complexity of Radix Sort using base 10 counting sort for each digit?
Attempts:
2 left
💡 Hint
Counting sort runs in O(n + k) where k is the range of digits (10 here).
✗ Incorrect
Radix Sort runs counting sort d times. Each counting sort is O(n + 10) = O(n). So total is O(d * n).
