Data Structures & AlgorithmsVery hardGoldman Sachs OA · LeetCode medium-hard

QuickSort (last element as pivot, Lomuto-style partition) sorts [31, 34, 38, 59, 23, 16, 54, 42, 46]. Counting one comparison per element checked against the pivot during partitioning, how many comparisons does it perform in total across the whole recursion?

  1. A. 24
  2. B. 36
  3. C. 29
  4. D. 33

Correct answer: A. 24

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 24 comparisons. 36 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.

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

Exams that ask this topic