← Amazon Interview Insights

Amazon·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Jun 2026

Summary

Amazon SWE coding round, one problem the whole time. Classic array merging in-place, which sounds straightforward until you're actually on the clock and second-guessing your pointer logic.

Questions Asked (1)

Q1

Given two sorted ascending arrays, merge the second into the first in-place so the result stays sorted. No extra arrays allowed. The first array has enough trailing space to fit everything.

Algorithms & Data Structures
Author's notes

I knew this problem.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a three-pointer technique: one pointer at the last valid element of the first array, one at the last element of the second array, and one at the last position of the first array's total capacity. Compare elements from the end and place the larger one at the write pointer, moving pointers backward until all elements are merged. This avoids shifting and achieves O(m+n) time with O(1) extra space.

Pro tip: Emphasize that merging from the end is the key insight to avoid overwriting unmerged elements, and mention that this approach is optimal for in-place merging. Also, clarify edge cases like when the second array is empty or when all elements of the second array are smaller than the first array's elements.

1. Clarify the problem and constraints

Confirm that the first array has enough trailing space (e.g., filled with zeros or placeholders) and that both arrays are sorted ascending. Ask if the arrays can contain duplicates and if stability matters.

2. Define pointers and initialize

Let m be the number of valid elements in the first array, n be the length of the second array. Set i = m-1 (last valid in first), j = n-1 (last in second), and k = m+n-1 (last position in first array's total capacity).

3. Merge from the end

While i >= 0 and j >= 0, compare nums1[i] and nums2[j]. Place the larger at nums1[k], then decrement the corresponding pointer and k. This ensures we never overwrite unprocessed elements.

4. Handle remaining elements

If j >= 0 after the loop, copy remaining elements from nums2 into nums1[0..j]. If i >= 0, they are already in place, so no action needed.

5. Analyze complexity and edge cases

State time complexity O(m+n) and space O(1). Discuss edge cases: nums2 empty, nums1 empty (m=0), all elements of nums2 smaller than nums1, and duplicates.

Key Points to Mention

  • Three-pointer technique: i for first array's last valid element, j for second array's last element, k for write position at the end.
  • Merging from the end avoids overwriting unmerged elements, which is crucial for in-place merging.
  • Time complexity O(m+n) and space complexity O(1).
  • Edge cases: empty second array, empty first array (m=0), all elements of second array smaller than first array's elements.
  • Comparison logic: place larger element at nums1[k] and move pointers accordingly.
  • After main loop, only need to copy remaining elements from second array if any; first array's remaining elements are already in correct place.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.