Reported February 2020
Bloomberglinked list

Sort an Absolute-Value-Sorted Linked List

Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Bloomberg OA. Under 2s to a working solution.
Founder's read

Bloomberg reported this one in February 2020, and it looks scarier than it is. The list is sorted by absolute value, and you need it sorted by real value. That reduces to one idea: the negatives are already in the right relative order, just backwards. Pull them out, reverse them, and merge with the positives in one pass. It's a linked-list problem dressed up as a sorting problem. If you blank on the pointer work during the live OA, StealthCoder runs invisibly on your desktop as a safety net and gives you the solution in real time.

The problem

A singly linked list is sorted by nondecreasing absolute value. Rearrange its existing nodes so their integer values are in ordinary nondecreasing order and return the new head.

Function
sortAbsoluteList(head: ListNode) → ListNode

Examples
Example 1
head = [1,-2,-3,4,-5]
return = [-5,-3,-2,1,4]
Negative nodes reverse magnitude order before the positive nodes.
Example 2
head = [-1,-2,-3]
return = [-3,-2,-1]
All negative nodes reverse.

Constraints
The list has at most 10^5 nodes.
Absolute values are nondecreasing along the input list.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Here's the trick. Walk the list once and split nodes into two chains: non-negatives kept in original order, and negatives pushed onto the front of a negative chain. Pushing to the front reverses them as you go, so the most negative value ends up first. Then the negative chain is already ascending and the non-negative chain is already ascending. Join them by pointing the tail of the negatives at the head of the non-negatives. That's O(n) time and O(1) extra space, since you only relink existing nodes. The common pitfall is saving the next pointer too late and losing the rest of the list, or forgetting to null out the tail of the non-negative chain. Zeros go with the non-negatives. Test with all negatives, all positives, and a single node. StealthCoder is your hedge if the relinking logic slips under pressure.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

You can drill Sort an Absolute-Value-Sorted Linked List 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 would have shipped this the night before his JPMorgan OA if he'd had it.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Bloomberg's OA.

Bloomberg reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Sort an Absolute-Value-Sorted Linked List FAQ

What's the trick for the Bloomberg absolute-value sorted list problem?+

Negatives appear in decreasing value order relative to what you want, so reversing them fixes it. Split into a negative chain and a non-negative chain, front-insert negatives as you go, then concatenate. No general sorting needed, just one pass.

How hard is this really?+

Easier than it looks. It's a medium-feeling problem with an easy core. The only real difficulty is careful pointer handling. If you can reverse a linked list and merge two lists, you have every piece you need.

Can I just collect values into an array and sort?+

You could, but the problem says to rearrange existing nodes, and sorting costs O(n log n). The single-pass relink is O(n) and uses constant extra space. Use the intended approach so the solution matches what's asked.

What edge cases should I test?+

Test all negatives like [-1,-2,-3], all non-negatives, a single node, zeros mixed in, and a list with up to 10^5 nodes. Also confirm the last node's next pointer is null so you don't create a cycle.

How do I prepare in 48 hours?+

Practice reversing a linked list iteratively and merging two sorted lists until both are automatic. Then write this solution once from scratch using a dummy node approach. Those two skills cover the whole problem.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Bloomberg.

OA at Bloomberg?
Invisible during screen share
Get it