Data Structures & AlgorithmsHardAmazon · Microsoft coding round

Problem: Which of these sorting algorithms is guaranteed O(n log n) in the worst case AND is stable (preserves the relative order of equal elements)?

  1. A. Heap sort
  2. B. Quicksort (standard Lomuto/Hoare partitioning)
  3. C. Merge sort
  4. D. Selection sort

Correct answer: C. Merge sort

Merge sort always splits and merges in O(n log n) regardless of input, and a standard merge step can be implemented to prefer the left half on ties, keeping it stable. Quicksort's worst case is O(n^2) and heap sort, while O(n log n) worst case, is not stable.

More Coding Interview Problems questions

Exams that ask this topic