Challenge - 5 Problems
Merge Sort Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Merge Sort on a small array
What is the output of the following Rust code that uses merge sort on the array [4, 1, 3, 2]?
DSA Rust
fn merge_sort(arr: &mut [i32]) {
let mid = arr.len() / 2;
if mid == 0 {
return;
}
merge_sort(&mut arr[..mid]);
merge_sort(&mut arr[mid..]);
let mut ret = arr.to_vec();
merge(&arr[..mid], &arr[mid..], &mut ret[..]);
arr.copy_from_slice(&ret);
}
fn merge(left: &[i32], right: &[i32], ret: &mut [i32]) {
let mut left_i = 0;
let mut right_i = 0;
let mut ret_i = 0;
while left_i < left.len() && right_i < right.len() {
if left[left_i] <= right[right_i] {
ret[ret_i] = left[left_i];
left_i += 1;
} else {
ret[ret_i] = right[right_i];
right_i += 1;
}
ret_i += 1;
}
while left_i < left.len() {
ret[ret_i] = left[left_i];
left_i += 1;
ret_i += 1;
}
while right_i < right.len() {
ret[ret_i] = right[right_i];
right_i += 1;
ret_i += 1;
}
}
fn main() {
let mut arr = [4, 1, 3, 2];
merge_sort(&mut arr);
println!("{:?}", arr);
}Attempts:
2 left
💡 Hint
Think about how merge sort divides and merges arrays in sorted order.
✗ Incorrect
Merge sort divides the array into halves recursively and merges them back in sorted order. The final sorted array is [1, 2, 3, 4].
🧠 Conceptual
intermediateTime complexity of Merge Sort
What is the time complexity of the merge sort algorithm in the average and worst cases?
Attempts:
2 left
💡 Hint
Consider how many times the array is divided and merged.
✗ Incorrect
Merge sort divides the array into halves (log n times) and merges all elements (n) each time, resulting in O(n log n) time complexity.
🔧 Debug
advancedIdentify the bug in this merge function
What error will occur when running this merge function in Rust?
DSA Rust
fn merge(left: &[i32], right: &[i32], ret: &mut [i32]) {
let mut left_i = 0;
let mut right_i = 0;
let mut ret_i = 0;
while left_i < left.len() && right_i < right.len() {
if left[left_i] < right[right_i] {
ret[ret_i] = left[left_i];
left_i += 1;
} else {
ret[ret_i] = right[right_i];
right_i += 1;
}
ret_i += 1;
}
while left_i < left.len() {
ret[ret_i] = left[left_i];
left_i += 1;
ret_i += 1;
}
while right_i < right.len() {
ret[ret_i] = right[right_i];
right_i += 1;
ret_i += 1;
}
}Attempts:
2 left
💡 Hint
Check the loop conditions carefully for off-by-one errors.
✗ Incorrect
The loop 'while left_i <= left.len()' allows left_i to equal left.len(), causing an index out of bounds panic when accessing left[left_i].
📝 Syntax
advancedCorrect Rust syntax for merge sort recursive call
Which option correctly calls merge_sort recursively on the left half of the array slice in Rust?
Attempts:
2 left
💡 Hint
Remember Rust requires mutable references for modifying slices.
✗ Incorrect
Option A passes a mutable reference to the left half slice, which is required for in-place sorting. Options A and D lack references, and C is an immutable reference.
🚀 Application
expertNumber of merge operations in merge sort
For an array of length 8, how many total merge operations does merge sort perform during the entire sorting process?
Attempts:
2 left
💡 Hint
Think about how many times the array is split and merged back.
✗ Incorrect
Merge sort splits the array into single elements and merges pairs back. For 8 elements, it performs 7 merges (one less than the number of elements).
