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 output of the following C# code after merging two sorted arrays without using extra space?
DSA C#
int[] arr1 = {1, 5, 9, 10, 15, 20}; int[] arr2 = {2, 3, 8, 13}; int n = arr1.Length; int m = arr2.Length; for (int i = 0; i < n; i++) { if (arr1[i] > arr2[0]) { int temp = arr1[i]; arr1[i] = arr2[0]; arr2[0] = temp; // Move arr2[0] to correct position to keep arr2 sorted int first = arr2[0]; int k; for (k = 1; k < m && arr2[k] < first; k++) { arr2[k - 1] = arr2[k]; } arr2[k - 1] = first; } } Console.WriteLine(string.Join(", ", arr1)); Console.WriteLine(string.Join(", ", arr2));
Attempts:
2 left
💡 Hint
Focus on how the smallest element of the second array is swapped and repositioned to keep both arrays sorted.
✗ Incorrect
The code swaps elements from the first array with the smallest element of the second array if the first array's element is bigger. Then it repositions the swapped element in the second array to maintain sorted order. This results in both arrays sorted without extra space.
🧠 Conceptual
intermediateUnderstanding the key step in merging without extra space
In the merge process without extra space, what is the main reason for repositioning the first element of the second array after swapping?
Attempts:
2 left
💡 Hint
Think about why the second array might become unsorted after swapping.
✗ Incorrect
After swapping, the first element of the second array might be larger than some elements following it, so repositioning it keeps the second array sorted.
🔧 Debug
advancedIdentify the bug in this merge code snippet
What error will this code produce when merging two sorted arrays without extra space?
DSA C#
int[] arr1 = {1, 4, 7, 8, 10}; int[] arr2 = {2, 3, 9}; int n = arr1.Length; int m = arr2.Length; for (int i = 0; i < n; i++) { if (arr1[i] > arr2[0]) { int temp = arr1[i]; arr1[i] = arr2[0]; arr2[0] = temp; int first = arr2[0]; int k = 1; while (k < m && arr2[k] < first) { arr2[k - 1] = arr2[k]; k++; } arr2[k - 1] = first; } } Console.WriteLine(string.Join(", ", arr1)); Console.WriteLine(string.Join(", ", arr2));
Attempts:
2 left
💡 Hint
Check the loop conditions and array accesses carefully.
✗ Incorrect
The code correctly repositions the swapped element in arr2 without exceeding array bounds, so no runtime error occurs.
❓ Predict Output
advancedResult after merging two sorted arrays with duplicates
What is the output after merging these two sorted arrays without extra space?
DSA C#
int[] arr1 = {1, 3, 5, 7}; int[] arr2 = {0, 3, 6, 8}; int n = arr1.Length; int m = arr2.Length; for (int i = 0; i < n; i++) { if (arr1[i] > arr2[0]) { int temp = arr1[i]; arr1[i] = arr2[0]; arr2[0] = temp; int first = arr2[0]; int k = 1; while (k < m && arr2[k] < first) { arr2[k - 1] = arr2[k]; k++; } arr2[k - 1] = first; } } Console.WriteLine(string.Join(", ", arr1)); Console.WriteLine(string.Join(", ", arr2));
Attempts:
2 left
💡 Hint
Duplicates can appear in both arrays after merging.
✗ Incorrect
The smallest elements are swapped and repositioned, resulting in arr1 = [0, 1, 3, 3] and arr2 = [5, 6, 7, 8].
🚀 Application
expertMinimum 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 standard in-place merge algorithm (as shown in previous examples) to merge them without extra space?
arr1 = [1, 4, 7, 8, 10]
arr2 = [2, 3, 9]
Attempts:
2 left
💡 Hint
Count how many times an element from arr1 is swapped with arr2[0].
✗ Incorrect
Swaps occur when arr1[i] > arr2[0]. For given arrays, swaps happen at i=1 (4>2), i=2 (7>3), i=3 (8>4), and i=4 (10>7), totaling 4 swaps.
