Bird
Raised Fist0
DSA Cprogramming~20 mins

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

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
In-Place 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 output of the following code after merging two sorted arrays without using extra space?
DSA C
void merge(int arr1[], int arr2[], int n, int m) {
    int i = 0, j = 0, k = n - 1;
    while (i <= k && j < m) {
        if (arr1[i] < arr2[j]) {
            i++;
        } else {
            int temp = arr2[j];
            arr2[j] = arr1[k];
            arr1[k] = temp;
            j++;
            k--;
        }
    }
    // Sort both arrays
    for (int x = 0; x < n - 1; x++) {
        for (int y = 0; y < n - x - 1; y++) {
            if (arr1[y] > arr1[y + 1]) {
                int temp = arr1[y];
                arr1[y] = arr1[y + 1];
                arr1[y + 1] = temp;
            }
        }
    }
    for (int x = 0; x < m - 1; x++) {
        for (int y = 0; y < m - x - 1; y++) {
            if (arr2[y] > arr2[y + 1]) {
                int temp = arr2[y];
                arr2[y] = arr2[y + 1];
                arr2[y + 1] = temp;
            }
        }
    }
}

int main() {
    int arr1[5] = {1, 5, 9, 10, 15};
    int arr2[4] = {2, 3, 8, 13};
    merge(arr1, arr2, 5, 4);
    // Print arr1
    for (int i = 0; i < 5; i++) {
        printf("%d ", arr1[i]);
    }
    printf("\n");
    // Print arr2
    for (int i = 0; i < 4; i++) {
        printf("%d ", arr2[i]);
    }
    return 0;
}
A1 2 3 5 8 \n 9 10 13 15 \n
B1 2 3 8 9 \n 5 10 13 15 \n
C1 3 5 8 9 \n 2 10 13 15 \n
D1 2 3 5 9 \n 8 10 13 15 \n
Attempts:
2 left
💡 Hint
Focus on swapping larger elements from arr1 with smaller elements from arr2, then sorting both arrays.
🧠 Conceptual
intermediate
1:30remaining
Understanding the in-place merge approach
Why does the algorithm swap elements between the end of the first array and the beginning of the second array during merging without extra space?
ATo reverse both arrays before merging them.
BTo move larger elements to the second array and smaller elements to the first array for easier sorting later.
CTo create a new array inside the function for merging.
DTo sort the arrays individually before merging.
Attempts:
2 left
💡 Hint
Think about how to keep the smallest elements in the first array without extra space.
🔧 Debug
advanced
2:00remaining
Identify the error in this merge function
What error will the following code produce when merging two sorted arrays without extra space?
DSA C
void merge(int arr1[], int arr2[], int n, int m) {
    int i = 0, j = 0, k = n - 1;
    while (i < k && j < m) {
        if (arr1[i] < arr2[j]) {
            i++;
        } else {
            int temp = arr2[j];
            arr2[j] = arr1[k];
            arr1[k] = temp;
            j++;
            k--;
        }
    }
    // Sorting code omitted for brevity
}
ACompilation error due to missing semicolon.
BArray index out of bounds when k becomes negative.
CNo error; code runs correctly.
DInfinite loop because condition i < k excludes i == k causing missed swaps.
Attempts:
2 left
💡 Hint
Check the loop condition carefully and consider when i equals k.
❓ Predict Output
advanced
2:30remaining
Output after merging arrays with duplicates
What is the output of the following code after merging two sorted arrays with duplicates without extra space?
DSA C
void merge(int arr1[], int arr2[], int n, int m) {
    int i = 0, j = 0, k = n - 1;
    while (i <= k && j < m) {
        if (arr1[i] <= arr2[j]) {
            i++;
        } else {
            int temp = arr2[j];
            arr2[j] = arr1[k];
            arr1[k] = temp;
            j++;
            k--;
        }
    }
    // Sort both arrays
    for (int x = 0; x < n - 1; x++) {
        for (int y = 0; y < n - x - 1; y++) {
            if (arr1[y] > arr1[y + 1]) {
                int temp = arr1[y];
                arr1[y] = arr1[y + 1];
                arr1[y + 1] = temp;
            }
        }
    }
    for (int x = 0; x < m - 1; x++) {
        for (int y = 0; y < m - x - 1; y++) {
            if (arr2[y] > arr2[y + 1]) {
                int temp = arr2[y];
                arr2[y] = arr2[y + 1];
                arr2[y + 1] = temp;
            }
        }
    }
}

int main() {
    int arr1[6] = {1, 3, 5, 7, 7, 9};
    int arr2[5] = {2, 3, 7, 8, 10};
    merge(arr1, arr2, 6, 5);
    for (int i = 0; i < 6; i++) {
        printf("%d ", arr1[i]);
    }
    printf("\n");
    for (int i = 0; i < 5; i++) {
        printf("%d ", arr2[i]);
    }
    return 0;
}
A1 3 3 5 7 7 \n 2 7 8 9 10 \n
B1 2 3 5 7 7 \n 3 7 8 9 10 \n
C1 2 3 3 5 7 \n 7 7 8 9 10 \n
D1 2 3 5 7 9 \n 3 7 7 8 10 \n
Attempts:
2 left
💡 Hint
Duplicates are handled by swapping and sorting; check how equal elements are compared.
🚀 Application
expert
2:00remaining
Minimum number of swaps to merge two sorted arrays without extra space
Given two sorted arrays arr1 and arr2, what is the minimum number of swaps performed by the in-place merge algorithm to merge them without extra space?
DSA C
void merge(int arr1[], int arr2[], int n, int m) {
    int i = 0, j = 0, k = n - 1;
    int swap_count = 0;
    while (i <= k && j < m) {
        if (arr1[i] <= arr2[j]) {
            i++;
        } else {
            int temp = arr2[j];
            arr2[j] = arr1[k];
            arr1[k] = temp;
            j++;
            k--;
            swap_count++;
        }
    }
    // Sorting code omitted
    printf("Swaps: %d\n", swap_count);
}

int main() {
    int arr1[5] = {1, 4, 7, 8, 10};
    int arr2[4] = {2, 3, 9, 15};
    merge(arr1, arr2, 5, 4);
    return 0;
}
A2
B3
C1
D4
Attempts:
2 left
💡 Hint
Count how many times elements from arr2 are smaller than elements at the end of arr1.

Practice

(1/5)
1. What is the main goal when merging two sorted arrays without extra space?
easy
A. Combine both arrays into one sorted array without using extra memory
B. Sort each array separately 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 task is to merge two sorted arrays into a single sorted sequence without using extra space, meaning no additional arrays or large buffers.
  2. Step 2: Identify the correct approach

    We must rearrange elements within the given arrays to maintain sorted order without allocating extra memory.
  3. Final Answer:

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

    Merge without extra space = Combine in place [OK]
Hint: Merge by swapping elements, no extra array needed [OK]
Common Mistakes:
  • Thinking extra array is allowed
  • Sorting arrays separately without merging
  • Copying elements without sorting
2. Which of the following C code snippets correctly declares two sorted arrays for merging without extra space?
easy
A. int arr1[] = (1, 3, 5); int arr2[] = (2, 4, 6);
B. int arr1 = {1, 3, 5}; int arr2 = {2, 4, 6};
C. int arr1[] = [1, 3, 5]; int arr2[] = [2, 4, 6];
D. int arr1[] = {1, 3, 5}; int arr2[] = {2, 4, 6};

Solution

  1. Step 1: Check array declaration syntax in C

    Arrays in C are declared using curly braces {} with type and square brackets [].
  2. Step 2: Validate each option

    int arr1[] = {1, 3, 5}; int arr2[] = {2, 4, 6}; uses correct syntax: int arr[] = {elements}; Options A, B, and D use invalid syntax for arrays in C.
  3. Final Answer:

    int arr1[] = {1, 3, 5}; int arr2[] = {2, 4, 6}; -> Option D
  4. Quick Check:

    C arrays use {} for initialization [OK]
Hint: Use curly braces {} to initialize arrays in C [OK]
Common Mistakes:
  • Using parentheses () or square brackets [] for initialization
  • Missing data type or brackets
  • Assigning arrays without braces
3. Given two sorted arrays:
arr1 = {1, 4, 7, 8, 10}
arr2 = {2, 3, 9}
After merging without extra space, what will be the content of arr1?
medium
A. [1, 2, 3, 4, 8]
B. [1, 2, 3, 4, 7]
C. [1, 2, 3, 7, 8]
D. [1, 3, 4, 7, 8]

Solution

  1. Step 1: Understand merging without extra space

    We merge by comparing elements and swapping to keep smallest elements in arr1.
  2. Step 2: Merge process

    Initial arr1: [1,4,7,8,10], arr2: [2,3,9]
    Compare 10 (arr1 last) and 2 (arr2 first), swap -> arr1: [1,4,7,8,2], arr2: [10,3,9]
    Sort arr2: [3,9,10]
    Repeat swaps and sorts until arr1 holds smallest 5 elements:
    Final arr1: [1,2,3,4,7]
  3. Final Answer:

    [1, 2, 3, 4, 7] -> Option B
  4. Quick Check:

    Smallest 5 elements in arr1 after merge = [1,2,3,4,7] [OK]
Hint: Keep smallest elements in first array by swapping [OK]
Common Mistakes:
  • Not sorting arr2 after swaps
  • Assuming arr1 remains unchanged
  • Mixing elements without sorting
4. Identify the error in this code snippet for merging two sorted arrays without extra space:
for (int i = 0; i < n; i++) {
  if (arr1[i] > arr2[0]) {
    int temp = arr1[i];
    arr1[i] = arr2[0];
    arr2[0] = temp;
    sort(arr2, arr2 + m);
  }
}
medium
A. Using sort() without including <algorithm> header
B. Swapping elements incorrectly between arrays
C. Sorting arr2 inside the loop causes inefficiency
D. Loop should run from 0 to m instead of n

Solution

  1. Step 1: Analyze the loop and sorting

    The code swaps elements and sorts arr2 inside the loop for every iteration.
  2. Step 2: Identify inefficiency

    Sorting arr2 in every iteration is costly and inefficient; better to optimize sorting or use a different approach.
  3. Final Answer:

    Sorting arr2 inside the loop causes inefficiency -> Option C
  4. Quick Check:

    Sorting inside loop = inefficient [OK]
Hint: Avoid sorting inside loops for better performance [OK]
Common Mistakes:
  • Ignoring performance impact of repeated sorting
  • Confusing array sizes for loop limits
  • Assuming swap logic is incorrect
5. You have two sorted arrays:
arr1 = {1, 5, 9, 10, 15, 20}
arr2 = {2, 3, 8, 13}
Using the gap method to merge without extra space, what is the final state of arr2 after merging?
hard
A. [10, 13, 15, 20]
B. [8, 9, 10, 15]
C. [8, 13, 15, 20]
D. [9, 10, 13, 15]

Solution

  1. Step 1: Understand the gap method

    The gap method compares elements at a gap distance and swaps if out of order, reducing gap until 1.
  2. Step 2: Apply gap method to arrays

    Initial arr1: [1,5,9,10,15,20], arr2: [2,3,8,13]
    After merging, arr1 holds smallest elements: [1,2,3,5,8,9]
    arr2 holds remaining elements: [10,13,15,20]
  3. Step 3: Final arr2 state

    arr2 after merge is [10, 13, 15, 20].
  4. Final Answer:

    [10, 13, 15, 20] -> Option A
  5. Quick Check:

    Gap method final arr2 = [10, 13, 15, 20] [OK]
Hint: Gap method sorts by comparing distant elements [OK]
Common Mistakes:
  • Confusing final positions of elements in arr2
  • Assuming arr2 remains unchanged
  • Misapplying gap calculation
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