My first instinct was to start from the front and shift elements right, which is the obvious wrong move because you end up doing a ton of unnecessary work.
Use a three-pointer technique starting from the end of both arrays to fill the first array from back to front, avoiding the need to shift elements. Compare the largest remaining elements and place the larger one at the current end position, then move the pointers accordingly. This achieves O(m+n) time and O(1) extra space.
Pro tip: Clarify that the first array has exactly enough extra space to hold all elements of the second array, and handle edge cases like one array being empty. Mention that merging from the end is optimal because it avoids overwriting unmerged elements.
Confirm that the first array has length m+n with n extra zeros at the end, and the second array has length n. The goal is to merge in-place without using extra space.
Set three pointers: i = m-1 (last valid element in first array), j = n-1 (last element in second array), and k = m+n-1 (last position in first array).
While i >= 0 and j >= 0, compare nums1[i] and nums2[j]. Place the larger value at nums1[k], then decrement the corresponding pointer and k.
If j >= 0 after the loop, copy the remaining elements from nums2 into nums1. If i >= 0, the remaining elements are already in place.
State that time complexity is O(m+n) and space complexity is O(1). Discuss edge cases: one array empty, all elements of one array smaller than the other, etc.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.