Data Structures & AlgorithmsVery hardProduct placement · Amazon/Microsoft
Top-down merge sort (split at mid = n // 2, recurse, then merge, stopping comparisons when one side runs out) sorts [10, 29, 19, 28, 32, 42, 4, 44]. How many key comparisons does it make?
- A. 15
- B. 24
- C. 17
- D. 28
Correct answer: A. 15
Adding the comparisons of the seven merges gives 15; a merge stops as soon as one side runs out, so early exhaustion saves comparisons. 17 is the worst case for n = 8, 24 also counts the leftover copies, and 28 = n(n − 1)/2 is a quadratic sort.
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
- The Boyer–Moore majority vote (candidate/counter, no verification pass) runs on [3, 3, 3, 3, 7, 3, 7, 5, 5]. What candidate and final…
- A prefix-sum array P of A (P[0] = 0, P[i] = A[0] + … + A[i − 1]) is built. Which expression gives the sum A[l] + … + A[r] (0-based…
- A peak is an element larger than its neighbours (array ends count as −∞). Binary search runs on [16, 19, 40, 1, 18, 30, 54, 59, 45]: while…
- The median-of-medians (groups of 5) selection algorithm is used to choose a pivot. What does it guarantee?
- Rotate a linked list to the RIGHT by k places (k may exceed the length). What is printed? python def rotate(h, k): n, t = 1, h while t.nx…
- Over all left rotations of [7, 7, 1, 1, 3, 6], what is the maximum value of Σ i·a[i] (0-based)? (Use R(j) = R(j − 1) + sum − n·a[n − j] to…