Implement Merge Sort
Reported by candidates from Salesforce's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Salesforce reported this one in September 2026, and it's about as plain as an OA gets. Implement Merge Sort. Under the name, it's a merge of two sorted halves with two pointers, wrapped in a recursive split. If your OA invite lands in the next day or two, expect to write this from memory without the library sort. StealthCoder sits invisibly on your screen as a safety net if your mind goes blank on the merge step mid-assessment. The rest of this page is the script: what it reduces to, where people slip, and how to get it right in one pass.
The problem
Given an integer array nums, return a new array containing the same values in nondecreasing order. Implement merge sort: recursively split the range, sort both halves, and merge the sorted halves. Function mergeSort(nums: int[]) → int[] Examples Example 1 nums = [5,2,3,1] return = [1,2,3,5] The two sorted halves merge into nondecreasing order. Example 2 nums = [5,1,1,2,0,0] return = [0,0,1,1,2,5] Duplicate values are retained. Constraints 0 <= nums.length <= 100000 -1000000000 <= nums[i] <= 1000000000
Reported by candidates. Source: FastPrep
Pattern and pitfall
The problem reduces to one thing: merging two already sorted arrays with two pointers. Split at the midpoint, recurse on each half, then walk both halves with indices i and j, always copying the smaller value into the output. Use <= when comparing so equal values keep their order and duplicates stay, as in Example 2. After the loop, copy whatever is left from either half. Common pitfalls: forgetting the base case for length 0 or 1, since nums.length can be 0, and slicing arrays at every level so you waste memory. With up to 100000 elements, recursion depth is only about 17, so stack overflow isn't a worry. Values reach 1e9 in magnitude, so just compare them directly. If you freeze on the leftover-copy step during the live OA, StealthCoder can supply the working merge. Time is O(n log n), space O(n).
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Implement Merge Sort cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as sort an array. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Salesforce's OA.
Salesforce reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Implement Merge Sort FAQ
What's the trick in the Salesforce Implement Merge Sort question?+
There's no hidden trick. It's two pointers over two sorted halves. Split at the middle, sort each half recursively, then merge by repeatedly taking the smaller front element. Handle the leftover tail after one half runs out. Get that merge loop right and everything else follows.
Can I just call the built-in sort?+
The problem says to implement merge sort with recursive splitting and merging, so don't count on a built-in sort being accepted. Even if the tests pass, the intent is clear. Write the recursion and the merge yourself to be safe.
How do I handle duplicates and empty input?+
Use <= when comparing the left and right front values so duplicates are kept and the sort stays stable. For empty input, your base case should return immediately when the length is 0 or 1. Example 2 has duplicates like 0,0 and 1,1, and your output must keep them all.
Will this pass the size limit of 100000 elements?+
Yes. Merge sort runs in O(n log n), roughly 1.7 million basic steps at that size. Recursion depth is about 17, so no stack issues. Avoid repeated array copying at every level if you can, but even simple slicing should be fine.
How do I prepare for this in 48 hours?+
Write merge sort from scratch three times without looking. Focus on the merge function: two indices, compare, append, then drain leftovers. Test it on an empty array, a single element, and the duplicate example. Once you can do it cold, you're done.