Data Structures & AlgorithmsHardProduct placement · Amazon/Microsoft

nums1 = [2, 5, 8, 0, 0, 0] (3 real values plus 3 buffer slots) and nums2 = [3, 6, 9] are merged in place by filling nums1 from the BACK with three pointers, stopping once nums2 is exhausted. How many writes into nums1 are made, and is filling from the back necessary to avoid overwriting unread values?

  1. A. 5 writes; yes
  2. B. 6 writes; yes
  3. C. 5 writes; no
  4. D. 4 writes; yes

Correct answer: A. 5 writes; yes

Writing from the back always lands in a slot that is free or already consumed. The loop stops once nums2 is used up; 2, the smallest real value of nums1, never moves, so there are 5 writes, not 6.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic