The key move is starting from the back of both arrays instead of the front.
Use a three-pointer technique starting from the end of both arrays: compare the largest remaining elements and place the larger one at the end of A's buffer, moving backwards. This avoids overwriting unmerged elements in A and achieves O(m+n) time and O(1) space.
Pro tip: Emphasize that merging from the end is the key insight to avoid extra space; also mention edge cases like empty arrays and that the algorithm is stable if you choose B's element when equal.
Confirm that A has length m+n with the last n slots empty, B has length n, and both are sorted. State that the goal is to merge in-place with O(m+n) time and O(1) extra space.
Set i = m-1 (last valid element in A), j = n-1 (last element in B), and k = m+n-1 (last position in A's buffer).
While i >= 0 and j >= 0, compare A[i] and B[j]; place the larger at A[k], then decrement the corresponding pointer and k. If equal, prefer B[j] to maintain stability.
If j >= 0 after the loop, copy remaining B elements into A[0..j]. If i >= 0, the remaining A elements are already in place.
Explain that each step places the largest remaining element in its final position, so the array remains sorted. Time is O(m+n) because each element is processed once; space is O(1) since only pointers are used.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.