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 after sorting the array using merge sort?
DSA Rust
fn merge_sort(mut arr: Vec<i32>) -> Vec<i32> {
if arr.len() <= 1 {
return arr;
}
let mid = arr.len() / 2;
let left = merge_sort(arr[..mid].to_vec());
let right = merge_sort(arr[mid..].to_vec());
merge(left, right)
}
fn merge(left: Vec<i32>, right: Vec<i32>) -> Vec<i32> {
let mut result = Vec::new();
let mut i = 0;
let mut j = 0;
while i < left.len() && j < right.len() {
if left[i] < right[j] {
result.push(left[i]);
i += 1;
} else {
result.push(right[j]);
j += 1;
}
}
while i < left.len() {
result.push(left[i]);
i += 1;
}
while j < right.len() {
result.push(right[j]);
j += 1;
}
result
}
fn main() {
let arr = vec![3, 1, 4, 1, 5];
let sorted = merge_sort(arr);
println!("{:?}", sorted);
}Attempts:
2 left
💡 Hint
Think about how merge sort splits and merges arrays in sorted order.
✗ Incorrect
Merge sort splits the array into halves recursively and merges them back in sorted order. The final output is the sorted array.
❓ Predict Output
intermediateResult of merging two sorted vectors
What is the output of merging these two sorted vectors using the merge function?
DSA Rust
fn merge(left: Vec<i32>, right: Vec<i32>) -> Vec<i32> {
let mut result = Vec::new();
let mut i = 0;
let mut j = 0;
while i < left.len() && j < right.len() {
if left[i] < right[j] {
result.push(left[i]);
i += 1;
} else {
result.push(right[j]);
j += 1;
}
}
while i < left.len() {
result.push(left[i]);
i += 1;
}
while j < right.len() {
result.push(right[j]);
j += 1;
}
result
}
fn main() {
let left = vec![2, 4, 6];
let right = vec![1, 3, 5];
let merged = merge(left, right);
println!("{:?}", merged);
}Attempts:
2 left
💡 Hint
Merging two sorted lists results in a sorted combined list.
✗ Incorrect
The merge function compares elements from both lists and appends the smaller one, resulting in a fully sorted merged list.
🔧 Debug
advancedIdentify the bug in this merge sort implementation
What error will this Rust code produce when running the merge_sort function?
DSA Rust
fn merge_sort(arr: Vec<i32>) -> Vec<i32> {
if arr.len() <= 1 {
return arr;
}
let mid = arr.len() / 2;
let left = merge_sort(arr[..mid].to_vec());
let right = merge_sort(arr[mid..].to_vec());
merge(left, right)
}
fn merge(left: Vec<i32>, right: Vec<i32>) -> Vec<i32> {
let mut result = Vec::new();
let mut i = 0;
let mut j = 0;
while i < left.len() && j < right.len() {
if left[i] <= right[j] {
result.push(left[i]);
i += 1;
} else {
result.push(right[j]);
j += 1;
}
}
while i < left.len() {
result.push(left[i]);
i += 1;
}
while j < right.len() {
result.push(right[j]);
j += 1;
}
result
}
fn main() {
let arr = vec![3, 2, 1];
let sorted = merge_sort(arr);
println!("{:?}", sorted);
}Attempts:
2 left
💡 Hint
Check if the slicing and merging logic is correct and safe.
✗ Incorrect
The code correctly slices the vector and merges sorted halves without errors, producing sorted output.
❓ Predict Output
advancedOutput of merge sort on an empty vector
What is the output of the merge_sort function when given an empty vector?
DSA Rust
fn merge_sort(arr: Vec<i32>) -> Vec<i32> {
if arr.len() <= 1 {
return arr;
}
let mid = arr.len() / 2;
let left = merge_sort(arr[..mid].to_vec());
let right = merge_sort(arr[mid..].to_vec());
merge(left, right)
}
fn merge(left: Vec<i32>, right: Vec<i32>) -> Vec<i32> {
let mut result = Vec::new();
let mut i = 0;
let mut j = 0;
while i < left.len() && j < right.len() {
if left[i] < right[j] {
result.push(left[i]);
i += 1;
} else {
result.push(right[j]);
j += 1;
}
}
while i < left.len() {
result.push(left[i]);
i += 1;
}
while j < right.len() {
result.push(right[j]);
j += 1;
}
result
}
fn main() {
let arr: Vec<i32> = vec![];
let sorted = merge_sort(arr);
println!("{:?}", sorted);
}Attempts:
2 left
💡 Hint
Consider the base case for empty or single-element vectors.
✗ Incorrect
The base case returns the vector as is if its length is 0 or 1, so an empty vector returns empty.
🚀 Application
expertNumber of recursive calls in merge sort
Given an input vector of length 8, how many total calls to merge_sort (including the initial call) will be made during the full sorting process?
Attempts:
2 left
💡 Hint
Think about how merge sort splits the array into halves recursively until single elements.
✗ Incorrect
Merge sort calls merge_sort once per subarray. For length 8, it splits into 8 single elements, resulting in 15 calls total (2*n - 1).
