Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
HAPPY CASE
Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
Output: [1,2,2,3,5,6]
Explanation: The arrays we are merging are [1,2,3] and [2,5,6].
The result of the merge is [1,2,2,3,5,6] with the underlined elements coming from nums1.
Input: nums1 = [1], m = 1, nums2 = [], n = 0
Output: [1]
Explanation: The arrays we are merging are [1] and [].
The result of the merge is [1].
EDGE CASE
Input: nums1 = [0], m = 0, nums2 = [1], n = 1
Output: [1]
Explanation: The arrays we are merging are [] and [1].
The result of the merge is [1].
Note that because m = 0, there are no elements in nums1. The 0 is only there to ensure the merge result can fit in nums1.
Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.
For Array problems, we want to consider the following approaches:
⚠️ Common Mistakes
Plan the solution with appropriate visualizations and pseudocode.
General Idea: We can start at index m - 1 for nums1 and index n -1 for nums2, find the larger number and start inserting at m + n - 1 index of nums1. Repeat until we reach index 0 for nums2.
1. Initialize three pointers
a.point at last number in nums1
b.point at the last number of nums2
c. point at the last index in nums1
2) While nums2 has numbers to merge
a) Get the larger number and insert into the insertIndex of nums1
Implement the code to solve the algorithm.
class Solution:
def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
"
Do not return anything, modify nums1 in-place instead.
"
# Initialize three pointers
# a.point at last number in nums1
# b.point at the last number of nums2
# c. point at the last index in nums1
index1, index2, insertIndex = m - 1, n - 1, m + n - 1
# While nums2 has numbers to merge
while index2 >= 0:
# Get the larger number and insert into the insertIndex of nums1
if index1 >=0 and nums1[index1] > nums2[index2]:
nums1[insertIndex] = nums1[index1]
index1 -= 1
else:
nums1[insertIndex] = nums2[index2]
index2 -= 1
insertIndex -= 1
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
// Initialize three pointers
// a.point at last number in nums1
// b.point at the last number of nums2
// c. point at the last index in nums1
int tail1 = m - 1, tail2 = n - 1, insertIndex = m + n - 1;
// While nums2 has numbers to merge
while (tail2 >= 0) {
if (tail1 >= 0 && nums1[tail1] > nums2[tail2]) {
// Get the larger number and insert into the insertIndex of nums1
nums1[insertIndex] = nums1[tail1];
tail1--;
} else {
nums1[insertIndex] = nums2[tail2];
tail2--;
}
insertIndex--;
}
}
}
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume N
represents the number of items in the array nums1.
Assume M
represents the number of items in the array nums2.
O(N + M)
because we need to traverse all numbers in both array.O(1)
because we only need three pointers to do the job.