Merge Sorted Arrays In Place
Company: Verkada
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates array manipulation and in-place merging skills, specifically understanding sorted sequences, pointer/index management, and space-time trade-offs within array algorithms, and it falls under the Coding & Algorithms domain.
Constraints
- 0 <= m, n <= 100000
- len(A) == m + n
- len(B) == n
- -1000000000 <= A[i], B[i] <= 1000000000 for valid elements
- The first m elements of A are sorted in non-decreasing order
- B is sorted in non-decreasing order
Examples
Input: ([1,2,3,0,0,0], 3, [2,5,6], 3)
Expected Output: [1,2,2,3,5,6]
Explanation: Merge the two sorted lists [1,2,3] and [2,5,6]. The final sorted array is [1,2,2,3,5,6].
Input: ([1], 1, [], 0)
Expected Output: [1]
Explanation: There are no elements in B, so A stays unchanged.
Input: ([0], 0, [1], 1)
Expected Output: [1]
Explanation: A has no valid initial elements, so the result is just B copied into A.
Input: ([-3,-1,4,0,0,0], 3, [-2,2,2], 3)
Expected Output: [-3,-2,-1,2,2,4]
Explanation: After merging the sorted values from both arrays, the result is [-3,-2,-1,2,2,4].