Implement Merge Sort and Return a Sorted Array
Company: Salesforce
Role: Member of Technical Staff
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Overview: Implement merge sort from first principles and return integers in nondecreasing order without a built-in sort. The exercise covers empty input, duplicates, negative values, multiset preservation, divide-and-merge structure, and the expected logarithmic recursion depth.
Read the full Salesforce Member of Technical Staff interview experience this question came from
Constraints
- 0 <= values.length <= 200,000
- -10^9 <= values[i] <= 10^9
- The returned array contains exactly the same multiset as values.
- A built-in sorting function may not be called.
Examples
Input: ([],)
Expected Output: []
Explanation: The empty source example is already sorted.
Input: ([7],)
Expected Output: [7]
Explanation: A one-element subproblem needs no merge.
Hints
- Merge two sorted ranges with one pointer in each range.
- An iterative run width of 1, 2, 4, and so on avoids recursion while remaining merge sort.