Challenge - 5 Problems
Merge Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of merging two sorted arrays without extra space
What is the state of the two arrays after merging them without extra space using the given Rust code?
DSA Rust
fn merge(arr1: &mut [i32], arr2: &mut [i32]) {
let n = arr1.len();
let m = arr2.len();
let mut i = 0;
while i < n {
if arr1[i] > arr2[0] {
arr1[i] = std::mem::replace(&mut arr2[0], arr1[i]);
let mut first = arr2[0];
let mut k = 1;
while k < m && arr2[k] < first {
arr2[k - 1] = arr2[k];
k += 1;
}
arr2[k - 1] = first;
}
i += 1;
}
}
fn main() {
let mut arr1 = vec![1, 5, 9, 10, 15, 20];
let mut arr2 = vec![2, 3, 8, 13];
merge(&mut arr1, &mut arr2);
println!("arr1: {:?}", arr1);
println!("arr2: {:?}", arr2);
}Attempts:
2 left
💡 Hint
Focus on how the smallest elements are swapped and the second array is kept sorted.
✗ Incorrect
The algorithm swaps elements from arr1 with the smallest element in arr2 if arr1's element is greater. Then it repositions the swapped element in arr2 to keep arr2 sorted. After processing all elements, arr1 contains the smallest elements and arr2 the larger ones.
🧠 Conceptual
intermediateUnderstanding the time complexity of merging without extra space
What is the time complexity of merging two sorted arrays without extra space using the swapping and insertion method?
Attempts:
2 left
💡 Hint
Consider the nested loops: one iterating over arr1 and one shifting elements in arr2.
✗ Incorrect
For each element in arr1, the algorithm may shift multiple elements in arr2 to maintain sorted order, leading to O(n * m) time complexity.
🔧 Debug
advancedIdentify the bug causing incorrect merge result
Given the following Rust code snippet for merging two sorted arrays without extra space, which line causes the bug leading to incorrect final arrays?
DSA Rust
fn merge(arr1: &mut [i32], arr2: &mut [i32]) {
let n = arr1.len();
let m = arr2.len();
let mut i = 0;
while i < n {
if arr1[i] > arr2[0] {
arr1[i] = arr2[0];
let mut first = arr2[0];
let mut k = 1;
while k < m && arr2[k] < first {
arr2[k - 1] = arr2[k];
k += 1;
}
arr2[k - 1] = first;
}
i += 1;
}
}Attempts:
2 left
💡 Hint
Check how elements are swapped between arrays.
✗ Incorrect
The line arr1[i] = arr2[0]; overwrites arr1[i] without saving its original value, losing data. It should swap values to preserve both elements.
📝 Syntax
advancedIdentify the syntax error in the Rust merge function
Which option contains a syntax error in the Rust code for merging two sorted arrays without extra space?
DSA Rust
fn merge(arr1: &mut [i32], arr2: &mut [i32]) {
let n = arr1.len();
let m = arr2.len();
let mut i = 0;
while i < n {
if arr1[i] > arr2[0] {
arr1[i] = std::mem::replace(&mut arr2[0], arr1[i]);
let mut first = arr2[0];
let mut k = 1;
while k < m && arr2[k] < first {
arr2[k - 1] = arr2[k];
k += 1;
}
arr2[k - 1] = first;
}
i += 1;
}
}Attempts:
2 left
💡 Hint
Look carefully at line endings and Rust syntax rules.
✗ Incorrect
Rust requires semicolons after statements. The missing semicolon after the std::mem::replace call causes a syntax error.
🚀 Application
expertResulting arrays after merging with given inputs
Given arr1 = [4, 6, 8, 10] and arr2 = [1, 2, 3, 7, 9], what are the arrays after merging without extra space using the standard swapping method?
Attempts:
2 left
💡 Hint
Remember arr1 should hold the smallest elements after merging.
✗ Incorrect
The smallest four elements go to arr1 in sorted order, and the remaining elements go to arr2 sorted.
