Challenge - 5 Problems
Master of In-Place Array Merging
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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 Python
def merge(arr1, arr2, n, m): i = 0 j = 0 while i < n and j < m: if arr1[i] > arr2[j]: arr1[i], arr2[j] = arr2[j], arr1[i] i += 1 j += 1 else: i += 1 arr1.sort() arr2.sort() arr1 = [1, 5, 9, 10, 15, 20] arr2 = [2, 3, 8, 13] merge(arr1, arr2, len(arr1), len(arr2)) print(arr1) print(arr2)
Attempts:
2 left
💡 Hint
Focus on swapping elements when the first array element is greater than the second array element, then sort both arrays.
✗ Incorrect
The code swaps elements between arr1 and arr2 when arr1[i] is greater than arr2[j], then sorts both arrays. This results in the smallest elements in arr1 and the larger ones in arr2, maintaining sorted order without extra space.
🧠 Conceptual
intermediate1:30remaining
Understanding the in-place merge approach
Why does the approach of swapping elements between two sorted arrays and then sorting both arrays work for merging without extra space?
Attempts:
2 left
💡 Hint
Think about how swapping affects the distribution of smaller and larger elements.
✗ Incorrect
Swapping moves smaller elements to the first array and larger ones to the second. Sorting both arrays afterward ensures each array is sorted individually, achieving a merged state without extra space.
🔧 Debug
advanced2:00remaining
Identify the bug in the merge function
What is the bug in this merge function that merges two sorted arrays without extra space?
DSA Python
def merge(arr1, arr2, n, m): i = 0 j = 0 while i < n and j < m: if arr1[i] > arr2[j]: arr1[i], arr2[j] = arr2[j], arr1[i] j += 1 else: i += 1 arr1.sort() arr2.sort()
Attempts:
2 left
💡 Hint
Check the loop variables and how they change inside the loop.
✗ Incorrect
After swapping, only j is incremented but i remains the same, so if arr1[i] is still greater than arr2[j], the loop never progresses i, causing an infinite loop.
❓ Predict Output
advanced2:30remaining
Output of optimized gap method for merging arrays
What is the output of the following code that merges two sorted arrays without extra space using the gap method?
DSA Python
def next_gap(gap): if gap <= 1: return 0 return (gap // 2) + (gap % 2) def merge(arr1, arr2, n, m): gap = n + m gap = next_gap(gap) while gap > 0: i = 0 while i + gap < n: if arr1[i] > arr1[i + gap]: arr1[i], arr1[i + gap] = arr1[i + gap], arr1[i] i += 1 j = gap - n if gap > n else 0 while i < n and j < m: if arr1[i] > arr2[j]: arr1[i], arr2[j] = arr2[j], arr1[i] i += 1 j += 1 if j < m: j = 0 while j + gap < m: if arr2[j] > arr2[j + gap]: arr2[j], arr2[j + gap] = arr2[j + gap], arr2[j] j += 1 gap = next_gap(gap) arr1 = [10, 27, 38, 43] arr2 = [3, 9, 82] merge(arr1, arr2, len(arr1), len(arr2)) print(arr1) print(arr2)
Attempts:
2 left
💡 Hint
The gap method compares elements at a distance and reduces the gap gradually.
✗ Incorrect
The gap method compares and swaps elements at a certain gap distance in both arrays, reducing the gap until zero. This results in fully merged sorted arrays without extra space.
🧠 Conceptual
expert2:00remaining
Why is the gap method efficient for merging without extra space?
Why does the gap method provide an efficient way to merge two sorted arrays without extra space compared to the naive swapping and sorting approach?
Attempts:
2 left
💡 Hint
Think about how reducing the gap affects the sorting process.
✗ Incorrect
The gap method reduces the gap between compared elements gradually, which reduces the total number of swaps and comparisons compared to sorting the arrays fully after swapping. It efficiently merges without extra space.