Sorted Absolute-Difference Sums Across Cyclic Shifts
Reported by candidates from ByteDance's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The constraint that matters here is n <= 200, and it's the whole story. ByteDance reported this one in August 2026, and it looks scarier than it is. You get two arrays, you rotate one through every cyclic right shift, sum the absolute differences against the other, then sort all n results. It's an array simulation problem with a sort at the end. If you're taking the OA in the next day or two, read the trick below. StealthCoder sits invisibly on your screen as a safety net if you blank on the indexing mid-assessment.
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 sortedCyclicShiftDifferences(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
Brute force is the intended solution. With n capped at 200, you have n shifts and n comparisons per shift, so about 40,000 operations. Add a sort of 200 values and you're done. Don't hunt for a clever prefix-sum or convolution trick, it isn't needed. The real pitfalls are small. First, the index formula: use nums1[(i - s + n) % n], and the + n keeps the modulo non-negative. Second, overflow. Values reach 10^9 in magnitude, so one difference can hit 2*10^9, which breaks a 32-bit int. Use long for the difference and the running sum. Third, sort the results, not the inputs. Check Example 2 by hand: shift 0 gives 2, shift 1 gives 0, sorted is [0, 2]. If the modulo or the overflow trips you live, StealthCoder can hand you the clean loop while you keep typing.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
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 would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass ByteDance's OA.
ByteDance 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.
Sorted Absolute-Difference Sums Across Cyclic Shifts FAQ
How hard is the ByteDance cyclic shift difference problem really?+
Easy to medium-easy. The n <= 200 limit means a double loop is fine. The difficulty is only in getting the shift index right and using 64-bit math. If you can write a nested loop and call sort, you can solve it.
What's the trick to this problem?+
There isn't a hidden one. 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 sum, store it, then sort the list of sums. Simulation is the pattern.
Why does the input size matter so much here?+
With n at most 200, an O(n^2) solution does roughly 40,000 steps, which is trivial. People waste time chasing O(n log n) ideas when the constraints already tell you brute force passes. Read the limits first, then decide.
What bugs break most submissions?+
Integer overflow and a negative modulo. Differences can reach 2*10^9, so use long. And (i - s) can go negative, so add n before the modulo. Also remember to sort the output array, since shifts aren't produced in order.
How do I prepare for this in 48 hours?+
Write the double loop once from memory and test it on both examples. Example 1 should give [7, 13, 25, 25]. Then practice a few cyclic-index problems so the (i - s + n) % n pattern feels automatic. Don't over-study, the problem is small.