Challenge - 5 Problems
Sorting Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ā Predict Output
intermediateOutput of Comparison Based Sorting (Rust)
What is the output of this Rust code that uses a comparison based sorting method?
DSA Rust
fn main() {
let mut arr = [5, 3, 8, 1, 2];
arr.sort();
println!("{:?}", arr);
}Attempts:
2 left
š” Hint
Think about what the sort() method does to the array.
ā Incorrect
The sort() method in Rust sorts the array in ascending order using comparison based sorting algorithms like Timsort or Introsort.
ā Predict Output
intermediateOutput of Non Comparison Based Sorting (Counting Sort)
What is the output of this Rust code that implements a simple counting sort for small positive integers?
DSA Rust
fn counting_sort(arr: &mut [usize]) {
let max = *arr.iter().max().unwrap();
let mut count = vec![0; max + 1];
for &num in arr.iter() {
count[num] += 1;
}
let mut i = 0;
for num in 0..=max {
for _ in 0..count[num] {
arr[i] = num;
i += 1;
}
}
}
fn main() {
let mut arr = [4, 2, 2, 8, 3, 3, 1];
counting_sort(&mut arr);
println!("{:?}", arr);
}Attempts:
2 left
š” Hint
Counting sort counts occurrences and rebuilds the array in order.
ā Incorrect
Counting sort counts how many times each number appears and then writes them back in sorted order.
š§ Conceptual
advancedKey Difference Between Comparison Based and Non Comparison Based Sorting
Which statement correctly describes the key difference between comparison based and non comparison based sorting algorithms?
Attempts:
2 left
š” Hint
Think about how each type decides the order of elements.
ā Incorrect
Comparison based sorting compares elements directly; non comparison based sorting uses techniques like counting or grouping by digits without direct comparisons.
ā Predict Output
advancedOutput of Radix Sort (Non Comparison Based)
What is the output of this Rust code implementing a simplified radix sort for positive integers?
DSA Rust
fn counting_sort_for_radix(arr: &mut [u32], exp: u32) {
let n = arr.len();
let mut output = vec![0; n];
let mut count = vec![0; 10];
for &num in arr.iter() {
let index = ((num / exp) % 10) as usize;
count[index] += 1;
}
for i in 1..10 {
count[i] += count[i - 1];
}
for &num in arr.iter().rev() {
let index = ((num / exp) % 10) as usize;
output[count[index] - 1] = num;
count[index] -= 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_for_radix(arr, exp);
exp *= 10;
}
}
fn main() {
let mut arr = [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 from least significant to most significant.
ā Incorrect
Radix sort uses counting sort on each digit place, resulting in a fully sorted array in ascending order.
š§ Conceptual
expertTime Complexity Limits of Comparison Based Sorting
Which statement correctly describes the theoretical time complexity limit for comparison based sorting algorithms?
Attempts:
2 left
š” Hint
Think about the decision tree model for comparisons.
ā Incorrect
The lower bound for comparison based sorting is O(n log n) due to the number of comparisons needed to order n elements.
