Sort an Absolute-Value-Sorted Array
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The naive move on this Bloomberg OA, reported in November 2020, is to call sort and move on. That breaks the linear time requirement, and it's the edge case that gets people flagged. The array is already sorted by absolute value, so negatives appear in reverse numeric order and nonnegatives appear in the right order. Your job is to merge those two runs. If you've got the invite and 48 hours, learn this one shape cold. StealthCoder sits invisibly on your screen as a safety net if you blank during the live assessment, but the idea fits in your head.
The problem
nums is sorted by nondecreasing absolute value. Return its values in ordinary nondecreasing numeric order in linear time. Function sortAbsoluteArray(nums: int[]) → int[] Examples Example 1 nums = [0,-1,2,-4,5,6,-10,-13,-22] return = [-22,-13,-10,-4,-1,0,2,5,6] Negative values reverse their absolute-value order before merging with nonnegative values. Constraints 0 <= nums.length <= 10^5. Absolute values are nondecreasing.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: split the array into two sorted sequences. Negatives, read right to left, are already in ascending order when you want them. Nonnegatives, read left to right, are ascending too. Merge them like the merge step of merge sort, or use two pointers from the ends of the input. The pitfall is the boundary. Empty arrays, all negatives, all nonnegatives, and zeros all need to work. Don't rely on the negatives being contiguous at the start, because absolute-value order interleaves them with positives, as in [0,-1,2,-4,5]. So collect them into separate lists, reverse the negatives, then merge. That's O(n) time and O(n) space. Another route is a pointer at the last negative and one at the first nonnegative, picking the smaller each step. If you freeze during the live OA, StealthCoder is the hedge that gives you the merge structure back.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Sort an Absolute-Value-Sorted Array 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. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as squares of a sorted array. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Sort an Absolute-Value-Sorted Array FAQ
What's the trick to sorting this in linear time?+
Treat the input as two sorted lists in disguise. Negatives are descending in numeric order as you go left to right, so reading them backward gives ascending. Nonnegatives are already ascending. Merge the two lists with two pointers. No built-in sort needed.
Why can't I just call sort on the array?+
Sorting is O(n log n), and the problem explicitly asks for linear time. With up to 10^5 elements, the sort might still pass tests, but it misses the stated requirement and a reviewer could mark it down. The structure of the input is the whole point.
What edge cases should I test?+
Test an empty array, a single element, all negatives, all nonnegatives, and zeros mixed in. Also test duplicates like [-2,2] where absolute values tie. Make sure your merge handles one list running out before the other, since that's where off-by-one bugs show up.
How hard is this Bloomberg question really?+
It's easy to medium. The code is short, but you need to spot that this is a merge problem rather than a sort problem. Once you see two sorted runs, it's about fifteen lines. Most failures come from messy pointer handling, not from the idea.
How do I prepare for this in 48 hours?+
Write the two-pointer merge of two sorted arrays from memory until it's automatic. Then write this problem once with separate negative and nonnegative lists. Run your five edge cases by hand. That's enough. Don't spend your time on unrelated sorting variants.