Reported September 2026
Hudson River Tradingsimulation

Sorted Absolute-Difference Sums Across Cyclic Shifts

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

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

Hudson River Trading reported this one in September 2026, and the detail that matters is the index formula: nums1[(i - s + n) % n]. That's a plain cyclic right shift, and you need the sum of absolute differences against nums2 for every shift from 0 to n - 1. Then you sort the n sums. With n capped at 200, this is an array simulation problem wearing a fancy title. If you blank on the setup during the live OA, StealthCoder runs invisibly on your desktop as a safety net and hands you the structure. Most of the work is getting indexing and 64-bit sums right.

The problem

Given two integer arrays nums1 and nums2 of equal length n, consider every cyclic right shift of nums1.
For a shift of s positions, the value compared with nums2[i] is nums1[(i - s + n) % n]. Compute the sum of absolute pairwise differences for each shift s from 0 through n - 1.
Return all n sums sorted in nondecreasing order. Use 64-bit arithmetic for the differences and sums.

Function
solution(nums1: int[], nums2: int[]) → long[]

Examples
Example 1
nums1 = [1, 4, 2, 11]
nums2 = [10, 1, 8, 4]
return = [7, 13, 25, 25]
The four right shifts produce difference sums 25, 25, 13, and 7. Sorting them gives the returned array.
Example 2
nums1 = [1, 2]
nums2 = [2, 1]
return = [0, 2]
Without shifting, the sum is 2. Shifting right once produces [2, 1], whose sum is 0.

Constraints
1 <= nums1.length == nums2.length <= 200
-10^9 <= nums1[i], nums2[i] <= 10^9

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is noticing the constraints. n is at most 200, so the O(n^2) brute force is 40,000 operations. Don't hunt for a clever O(n log n) convolution-style approach. Loop s from 0 to n - 1, loop i from 0 to n - 1, add abs(nums1[(i - s + n) % n] - nums2[i]) into a long, store it, then sort the result. The pitfalls are practical. Values reach 10^9 in magnitude, so a single difference can hit 2*10^9 and overflow a 32-bit int. Cast before subtracting. Also check your shift direction against Example 2, where shifting right once turns [1, 2] into [2, 1]. If the live OA rattles you, StealthCoder is the hedge, but this one is easy to type from memory once you see the formula.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Sorted Absolute-Difference Sums Across Cyclic Shifts 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 StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Hudson River Trading's OA.

Hudson River Trading 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.

Sorted Absolute-Difference Sums Across Cyclic Shifts FAQ

How hard is this Hudson River Trading OA question really?+

Easy to medium. The statement looks intimidating, but n is at most 200, so brute force passes. The real risks are overflow and an off-by-one in the shift index. If you write the double loop cleanly, you're done in a few minutes.

What's the trick to solving it?+

There isn't a deep one. Use the given formula nums1[(i - s + n) % n] directly, sum absolute differences per shift in a 64-bit variable, then sort. The constraint of 200 elements tells you the O(n^2) approach is intended.

Where do people usually lose points on this?+

Integer overflow and shift direction. Differences can reach 2*10^9, which breaks a 32-bit int. People also shift left instead of right. Verify with Example 2: [1, 2] shifted right once becomes [2, 1], giving sum 0.

Do I need a faster algorithm than O(n^2)?+

No. With n up to 200, you do about 40,000 absolute-difference operations, then sort 200 numbers. Anything fancier adds bug risk without payoff. Save your effort for testing edge cases like n = 1 and negative values.

How do I prepare for this in 48 hours?+

Practice cyclic index math with modulo, especially the + n trick that avoids negative remainders. Write the double loop once, test both examples by hand, and confirm you return a long array sorted ascending. That covers nearly everything this problem tests.

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

OA at Hudson River Trading?
Invisible during screen share
Get it