Bird
Raised Fist0
DSA Rustprogramming~20 mins

Merge Two Sorted Arrays Without Extra Space in DSA Rust - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Merge Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output 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);
}
Aarr1: [1, 3, 5, 8, 9, 10], arr2: [2, 13, 15, 20]
Barr1: [1, 2, 3, 5, 8, 9], arr2: [10, 13, 15, 20]
Carr1: [1, 2, 3, 5, 8, 10], arr2: [9, 13, 15, 20]
Darr1: [1, 2, 3, 5, 8, 9], arr2: [13, 10, 15, 20]
Attempts:
2 left
💡 Hint
Focus on how the smallest elements are swapped and the second array is kept sorted.
🧠 Conceptual
intermediate
1:30remaining
Understanding 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?
AO(n * m), where n and m are the lengths of the two arrays
BO(n + m), linear time
CO(n log m), logarithmic insertion time
DO(n^2), quadratic time
Attempts:
2 left
💡 Hint
Consider the nested loops: one iterating over arr1 and one shifting elements in arr2.
🔧 Debug
advanced
2:00remaining
Identify 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;
    }
}
Alet mut first = arr2[0]; // should be mutable reference
Bwhile k < m && arr2[k] < first { ... } // incorrect loop condition
Carr2[k - 1] = first; // wrong placement of element
Darr1[i] = arr2[0]; // overwriting without swapping
Attempts:
2 left
💡 Hint
Check how elements are swapped between arrays.
📝 Syntax
advanced
1:30remaining
Identify 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;
    }
}
AMissing semicolon after std::mem::replace(&mut arr2[0], arr1[i])
BUsing 'while' instead of 'for' loop for k
CIncorrect function signature missing 'mut' keyword
DUsing 'let' inside while loop is invalid
Attempts:
2 left
💡 Hint
Look carefully at line endings and Rust syntax rules.
🚀 Application
expert
2:30remaining
Resulting 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?
Aarr1: [1, 2, 3, 4], arr2: [7, 6, 8, 9, 10]
Barr1: [1, 2, 3, 6], arr2: [4, 7, 8, 9, 10]
Carr1: [1, 2, 3, 4], arr2: [6, 7, 8, 9, 10]
Darr1: [1, 2, 3, 7], arr2: [4, 6, 8, 9, 10]
Attempts:
2 left
💡 Hint
Remember arr1 should hold the smallest elements after merging.

Practice

(1/5)
1. What is the main goal when merging two sorted arrays without extra space?
easy
A. Sort each array separately using extra space
B. Combine both arrays into one sorted sequence without using extra memory
C. Copy elements from one array to another without sorting
D. Create a new array to hold merged elements

Solution

  1. Step 1: Understand the problem requirement

    The problem asks to merge two sorted arrays into a sorted sequence without using extra space, meaning no new arrays can be created.
  2. Step 2: Identify the correct approach

    We must rearrange elements within the given arrays to maintain sorted order combined, without additional memory.
  3. Final Answer:

    Combine both arrays into one sorted sequence without using extra memory -> Option B
  4. Quick Check:

    Merge without extra space = Combine in place [OK]
Hint: Merge by swapping elements, no new arrays needed [OK]
Common Mistakes:
  • Trying to create a new array for merged result
  • Sorting arrays separately without merging
  • Ignoring the no extra space constraint
2. Which Rust syntax correctly declares two mutable sorted arrays for merging without extra space?
easy
A. let mut arr1 = [1, 3, 5]; let mut arr2 = [2, 4, 6];
B. let arr1 = [1, 3, 5]; let arr2 = [2, 4, 6];
C. mut arr1 = [1, 3, 5]; mut arr2 = [2, 4, 6];
D. let mut arr1 = (1, 3, 5); let mut arr2 = (2, 4, 6);

Solution

  1. Step 1: Identify correct Rust mutable array declaration

    In Rust, arrays are declared with let mut for mutability and square brackets for arrays.
  2. Step 2: Check each option

    let mut arr1 = [1, 3, 5]; let mut arr2 = [2, 4, 6]; uses let mut with arrays correctly. let arr1 = [1, 3, 5]; let arr2 = [2, 4, 6]; misses mut keyword. mut arr1 = [1, 3, 5]; mut arr2 = [2, 4, 6]; misses let keyword. let mut arr1 = (1, 3, 5); let mut arr2 = (2, 4, 6); uses tuples, not arrays.
  3. Final Answer:

    let mut arr1 = [1, 3, 5]; let mut arr2 = [2, 4, 6]; -> Option A
  4. Quick Check:

    Mutable arrays use let mut and [] [OK]
Hint: Use let mut and square brackets for mutable arrays [OK]
Common Mistakes:
  • Forgetting mut keyword for mutable arrays
  • Using tuples instead of arrays
  • Missing let keyword
3. What will be the content of arrays after merging arr1 = [1, 4, 7] and arr2 = [2, 5, 6] without extra space using the gap method?
medium
A. ]7 ,6 ,5[ = 2rra ,]4 ,2 ,1[ = 1rra
B. arr1 = [1, 4, 5], arr2 = [2, 6, 7]
C. rr1 = [1, 2, 4], arr2 = [5, 6, 7]
D. arr1 = [1, 2, 4], arr2 = [5, 6, 7]

Solution

  1. Step 1: Apply gap method to merge arrays

    Start with gap = ceil((3+3)/2) = 3. Compare and swap elements at gap distance across arrays until gap reduces to 0.
  2. Step 2: Final arrays after merging

    After applying gap method, arr1 becomes [1, 2, 4] and arr2 becomes [5, 6, 7], maintaining sorted order without extra space.
  3. Final Answer:

    arr1 = [1, 2, 4], arr2 = [5, 6, 7] -> Option D
  4. Quick Check:

    Gap method merges arrays in place [OK]
Hint: Use gap method to compare and swap elements at decreasing gaps [OK]
Common Mistakes:
  • Not reducing gap correctly
  • Swapping elements incorrectly across arrays
  • Confusing array indices during merge
4. Identify the error in this Rust code snippet for merging two sorted arrays without extra space:
let mut arr1 = [1, 5, 9];
let mut arr2 = [2, 6, 8];
for i in 0..arr1.len() {
    if arr1[i] > arr2[0] {
        let temp = arr1[i];
        arr1[i] = arr2[0];
        arr2[0] = temp;
    }
    arr2.sort();
}
medium
A. Using let temp causes ownership error in Rust
B. Swapping elements between arr1 and arr2 is not allowed
C. Sorting arr2 inside the loop is inefficient and incorrect
D. The loop range should be arr2.len() instead of arr1.len()

Solution

  1. Step 1: Analyze the loop and sorting inside it

    The code sorts arr2 inside the loop for every iteration, which is inefficient and unnecessary.
  2. Step 2: Correct approach for sorting arr2

    Sorting arr2 should be done once after all swaps, not inside the loop, to improve performance and correctness.
  3. Final Answer:

    Sorting arr2 inside the loop is inefficient and incorrect -> Option C
  4. Quick Check:

    Sort arr2 once after loop [OK]
Hint: Sort arr2 once after loop, not inside it [OK]
Common Mistakes:
  • Sorting arr2 repeatedly inside loop
  • Confusing loop ranges
  • Misunderstanding Rust ownership with temp variable
5. Given two sorted arrays arr1 = [1, 3, 5, 7] and arr2 = [0, 2, 6, 8, 9], how can you merge them without extra space so that after merging, arr1 contains the first 4 smallest elements and arr2 contains the remaining elements sorted? Choose the correct final arrays.
hard
A. arr1 = [0, 1, 2, 3], arr2 = [5, 6, 7, 8, 9]
B. arr1 = [1, 2, 3, 5], arr2 = [0, 6, 7, 8, 9]
C. arr1 = [0, 1, 3, 5], arr2 = [2, 6, 7, 8, 9]
D. arr1 = [0, 1, 3, 6], arr2 = [2, 5, 7, 8, 9]

Solution

  1. Step 1: Understand merging without extra space for unequal sizes

    We must rearrange elements so arr1 holds smallest 4 elements and arr2 holds the rest, both sorted.
  2. Step 2: Apply gap method or swapping approach

    By comparing and swapping elements between arrays and sorting arr2, final arrays become arr1 = [0, 1, 2, 3] and arr2 = [5, 6, 7, 8, 9].
  3. Final Answer:

    arr1 = [0, 1, 2, 3], arr2 = [5, 6, 7, 8, 9] -> Option A
  4. Quick Check:

    First 4 smallest in arr1, rest in arr2 [OK]
Hint: Place smallest elements in arr1 by swapping, then sort arr2 [OK]
Common Mistakes:
  • Not handling different array sizes correctly
  • Failing to keep both arrays sorted after merge
  • Assuming extra space is allowed
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard