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?
- A. 5 writes; yes
- B. 6 writes; yes
- C. 5 writes; no
- 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- What is the sum of the minimum value of every contiguous window of length 3 in the array [4, 2, 12, 11, -5, 3, 7]?
- For each index i, let di be (index of the nearest element to the right that is strictly greater than a[i]) minus i, or 0 if no such…
- How many permutations of 1, 2, 3, 4, 5 can be sorted into increasing order by a single stack, where each input element is pushed in order…
- In the Josephus game, 41 people stand in a circle numbered 1 to 41. Starting from person 1, every 3rd person is removed (counting…
- What is the area of the largest rectangle that fits in a histogram whose bar heights are 2, 1, 5, 6, 2, 3 (each bar has width 1)?
- Rain falls on the elevation map [4, 2, 0, 3, 2, 5] (each bar has width 1). How many units of water are trapped in total?