Challenge - 5 Problems
Merge Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after merging two sorted arrays without extra space
What is the output of the following Go code that merges two sorted arrays without using extra space?
DSA Go
package main import "fmt" func merge(arr1 []int, arr2 []int) { n, m := len(arr1), len(arr2) for i := 0; i < n; i++ { if arr1[i] > arr2[0] { arr1[i], arr2[0] = arr2[0], arr1[i] // Move the first element of arr2 to its correct position first := arr2[0] k := 1 for k < m && arr2[k] < first { arr2[k-1] = arr2[k] k++ } arr2[k-1] = first } } } func main() { arr1 := []int{1, 5, 9, 10, 15, 20} arr2 := []int{2, 3, 8, 13} merge(arr1, arr2) fmt.Println(arr1) fmt.Println(arr2) }
Attempts:
2 left
💡 Hint
Focus on how the smallest element of the second array is swapped and repositioned.
✗ Incorrect
The code swaps elements from arr1 with the smallest element of arr2 if arr1's element is bigger. Then it repositions the smallest element in arr2 to maintain sorted order. This results in both arrays sorted without extra space.
❓ Predict Output
intermediateResulting arrays after merging with different input
Given the following Go code merging two sorted arrays without extra space, what is the printed output?
DSA Go
package main import "fmt" func merge(arr1 []int, arr2 []int) { n, m := len(arr1), len(arr2) for i := 0; i < n; i++ { if arr1[i] > arr2[0] { arr1[i], arr2[0] = arr2[0], arr1[i] first := arr2[0] k := 1 for k < m && arr2[k] < first { arr2[k-1] = arr2[k] k++ } arr2[k-1] = first } } } func main() { arr1 := []int{10, 12} arr2 := []int{5, 18, 20} merge(arr1, arr2) fmt.Println(arr1) fmt.Println(arr2) }
Attempts:
2 left
💡 Hint
Check how the smallest element from arr2 moves into arr1 and arr2 is reordered.
✗ Incorrect
The smallest element 5 from arr2 swaps with 10 in arr1. Then arr2 is reordered to keep sorted order. Resulting arrays are [5 10] and [12 18 20].
❓ Predict Output
advancedOutput of merging arrays with duplicates
What will be the output of the following Go program that merges two sorted arrays containing duplicates without extra space?
DSA Go
package main import "fmt" func merge(arr1 []int, arr2 []int) { n, m := len(arr1), len(arr2) for i := 0; i < n; i++ { if arr1[i] > arr2[0] { arr1[i], arr2[0] = arr2[0], arr1[i] first := arr2[0] k := 1 for k < m && arr2[k] < first { arr2[k-1] = arr2[k] k++ } arr2[k-1] = first } } } func main() { arr1 := []int{1, 3, 5, 7} arr2 := []int{0, 3, 6, 8, 9} merge(arr1, arr2) fmt.Println(arr1) fmt.Println(arr2) }
Attempts:
2 left
💡 Hint
Duplicates are handled by swapping and repositioning the smallest element in arr2.
✗ Incorrect
The smallest element 0 swaps with 1 in arr1. Then 3 swaps with 3 but no change. The arrays after merging are [0 1 3 3] and [5 6 7 8 9].
🧠 Conceptual
advancedWhy does the merge algorithm reposition the first element of the second array?
In the in-place merge algorithm for two sorted arrays, why do we move the first element of the second array to its correct position after swapping?
Attempts:
2 left
💡 Hint
Think about what happens to the order of the second array after swapping.
✗ Incorrect
Swapping the first element of the second array with a bigger element from the first array can break the sorted order of the second array. Moving it to the correct position restores sorting.
❓ Predict Output
expertOutput of merging arrays with negative and positive numbers
What is the output of this Go program merging two sorted arrays containing negative and positive numbers without extra space?
DSA Go
package main import "fmt" func merge(arr1 []int, arr2 []int) { n, m := len(arr1), len(arr2) for i := 0; i < n; i++ { if arr1[i] > arr2[0] { arr1[i], arr2[0] = arr2[0], arr1[i] first := arr2[0] k := 1 for k < m && arr2[k] < first { arr2[k-1] = arr2[k] k++ } arr2[k-1] = first } } } func main() { arr1 := []int{-10, -3, 0, 5} arr2 := []int{-5, 2, 4, 6} merge(arr1, arr2) fmt.Println(arr1) fmt.Println(arr2) }
Attempts:
2 left
💡 Hint
Check how negative numbers are swapped and repositioned to maintain sorted order.
✗ Incorrect
The smallest element -5 swaps with -3 in arr1, then repositioned in arr2. The arrays become [-10 -5 -3 0] and [2 4 5 6].
