Data Structures & AlgorithmsHardAmazon/Microsoft SDE pattern
QuickSort (last element as pivot, Lomuto-style partition) sorts [57, 6, 37, 10, 15, 58]. Counting one comparison per element checked against the pivot during partitioning, how many comparisons does it perform in total across the whole recursion?
- A. 17
- B. 11
- C. 16
- D. 15
Correct answer: B. 11
Each partition call compares every other element in that subarray against the pivot once; summing these counts over the full recursion tree for this specific array and pivot choice gives 11 comparisons. 15 would be the comparison count for the worst case (already-sorted input with last-element pivot), which doesn't apply here since the array isn't already sorted.
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
- Using a sliding window (shrink from the left whenever more than k distinct characters are in the window), what is the length of the…
- The Dutch National Flag algorithm (single pass, three pointers low/mid/high) partitions [2, 0, 0, 1, 0, 1, 1, 0] (values 0, 1, 2 only) in…
- Array [87, 4, 31, 36, 38, 63, 75] is a sorted array rotated at pivot 6. Using the standard O(log n) rotated-array binary search (decide…
- Array [56, 73, 74, 82, 90, 3, 48] is a sorted array rotated at pivot 2. Using the standard O(log n) rotated-array binary search (decide…
- The Boyer–Moore majority vote algorithm (candidate/counter, no verification pass) runs on [1, 5, 5, 2, 5, 1, 5, 5]. What candidate and…
- The Boyer–Moore majority vote algorithm (candidate/counter, no verification pass) runs on [6, 4, 4, 4, 6, 4, 6, 2]. What candidate and…