Challenge - 5 Problems
Divide and Conquer Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Recursive Merge Sort Call Count
Consider the following Rust function that counts the number of recursive calls made during a merge sort on an array of size n. What is the output when calling
count_merge_sort_calls(8)?DSA Rust
fn count_merge_sort_calls(n: usize) -> usize {
if n <= 1 {
1
} else {
1 + count_merge_sort_calls(n / 2) + count_merge_sort_calls(n - n / 2)
}
}
fn main() {
println!("{}", count_merge_sort_calls(8));
}Attempts:
2 left
💡 Hint
Think about how many times the function splits the array and counts calls recursively.
✗ Incorrect
The function counts 1 for the current call plus the calls for the left and right halves. For n=8, the total calls sum to 15.
🧠 Conceptual
intermediateMaster Theorem Case Identification
Given the recurrence relation
T(n) = 3T(n/4) + n, which case of the Master Theorem applies and what is the asymptotic complexity?Attempts:
2 left
💡 Hint
Compare f(n) = n with n^{log_b a} where a=3 and b=4.
✗ Incorrect
log_4 3 ≈ 0.792, and since f(n) = n = n^{1} which is asymptotically larger than n^{0.792}, this fits Case 3 where f(n) is larger, so T(n) = Θ(n).
🔧 Debug
advancedIdentify the Error in Recursive Fibonacci Implementation
What error will the following Rust code produce when calling
fib(5)?DSA Rust
fn fib(n: i32) -> i32 {
if n == 0 {
0
} else if n == 1 {
1
} else {
fib(n - 1) + fib(n - 2)
}
}
fn main() {
println!("{}", fib(5));
}Attempts:
2 left
💡 Hint
Trace the recursive calls for fib(5).
✗ Incorrect
The code correctly implements Fibonacci recursively and returns 5 for fib(5). No errors occur.
❓ Predict Output
advancedOutput of QuickSort Partition Count
What is the output of the following Rust code that counts the number of partitions performed during QuickSort on the array
[3, 6, 2, 7, 5]?DSA Rust
fn quicksort_partition_count(arr: &mut [i32]) -> usize {
if arr.len() <= 1 {
0
} else {
let pivot = arr[arr.len() - 1];
let mut i = 0;
for j in 0..arr.len() - 1 {
if arr[j] < pivot {
arr.swap(i, j);
i += 1;
}
}
arr.swap(i, arr.len() - 1);
1 + quicksort_partition_count(&mut arr[0..i]) + quicksort_partition_count(&mut arr[i + 1..arr.len()])
}
}
fn main() {
let mut data = [3, 6, 2, 7, 5];
println!("{}", quicksort_partition_count(&mut data));
}Attempts:
2 left
💡 Hint
Count how many times the partition step is called recursively.
✗ Incorrect
The partition function is called once per subarray split. For this input, it is called 3 times in total.
🧠 Conceptual
expertRecurrence Relation for Strassen's Matrix Multiplication
Strassen's algorithm for multiplying two n×n matrices uses 7 recursive multiplications of size n/2 and additional work proportional to n^2. Which recurrence relation correctly models its time complexity?
Attempts:
2 left
💡 Hint
Recall how many recursive multiplications and the cost of combining results in Strassen's algorithm.
✗ Incorrect
Strassen's algorithm reduces the number of multiplications to 7 on subproblems of size n/2, with additional linear work in n^2 for additions and subtractions.
