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)?
- A. Heap sort
- B. Quicksort (standard Lomuto/Hoare partitioning)
- C. Merge sort
- 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
- Problem: Counting sort can sort n integers in the range [0, k] in O(n + k) time. What is the key limitation that makes it unsuitable for…
- Problem: A trie (prefix tree) storing n words with total length L supports which operation's time complexity most efficiently?
- Problem: Floyd's cycle detection ('tortoise and hare') finds whether a linked list has a cycle. What are its time and space complexity?
- Problem: What is the time complexity of a segment tree's range-query and point-update operations on an array of size n?
- Problem: Finding the median of two sorted arrays of sizes m and n can be done faster than merging them. What is the best known time…
- Problem: Which statement about the backtracking solution to generating all subsets of an n-element set is correct?