Maximize Adjacent Difference With One Reversal
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The edge case that kills a naive solution on this Amazon OA, reported in September 2026, is the reversal touching an array boundary. Most people brute-force every subarray, hit n up to 10^5, and watch it die. This is Maximize Adjacent Difference With One Reversal. It's an array problem where reversing a subarray only changes two boundary terms, not the interior. Once you see that, it collapses to a linear scan over pairs. If you blank during the live assessment, StealthCoder is the invisible safety net that reads the problem on screen and hands you the approach. Here's the trick first.
The problem
Given an integer array values, define its score as the sum of abs(values[i] - values[i + 1]) over every adjacent pair. You may reverse at most one contiguous subarray. Return the maximum score obtainable after the optional reversal. Function maximizeArrayScore(values: int[]) → long Examples Example 1 values = [2,3,1,5,4] return = 10 The original score is 8. Reversing the subarray [3,1,5] produces [2,5,1,3,4], whose score is 10. Example 2 values = [1,2,3,4] return = 5 Reversing [2,3] produces [1,3,2,4], with score 2 + 1 + 2 = 5. Constraints 1 <= values.length <= 10^5 -10^9 <= values[i] <= 10^9
Reported by candidates. Source: FastPrep
Pattern and pitfall
Reversing values[i..j] leaves every interior adjacent difference unchanged, since abs is symmetric. Only two edges change: abs(a[i-1]-a[i]) and abs(a[j]-a[j+1]) become abs(a[i-1]-a[j]) and abs(a[i])-a[j+1]) respectively. So you want the best gain over all i<j. Handle boundaries separately: reversing a prefix ending at j gains abs(a[0]-a[j+1]) - abs(a[j]-a[j+1]), and a suffix starting at i gains abs(a[i-1]-a[n-1]) - abs(a[i-1]-a[i]). For the interior case, use the classic max/min trick over pairs, tracking the max of min(a[k],a[k+1]) and the min of max(a[k],a[k+1]). The gain is 2*(maxOfMins - minOfMaxes) if positive. The pitfall is int overflow, so use 64-bit, and forgetting n=1 or n=2 where the answer is just the original score. If you freeze live, StealthCoder can cover you, but know the boundary cases cold.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Maximize Adjacent Difference With One Reversal 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 Amazon's OA.
Amazon 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.
Maximize Adjacent Difference With One Reversal FAQ
What's the trick in this Amazon OA question?+
Reversing a subarray only changes the two boundary differences. Interior pairs keep the same absolute value. So compute the original score once, then find the best gain from changing just two edges, instead of simulating every reversal.
Why does brute force fail here?+
There are O(n^2) subarrays and n goes up to 10^5. Even with an O(1) gain per candidate, that's roughly 10^10 pairs. You need a linear or near-linear method using the max-of-mins and min-of-maxes idea.
What edge cases should I test?+
Test length 1 and 2, where reversal can't help. Test reversals that start at index 0 or end at the last index, since they only have one boundary edge. Also test all-equal arrays, where the gain is zero, and extreme values near 10^9.
Do I need 64-bit integers?+
Yes. Each difference can reach 2*10^9, and summing up to 10^5 of them gets far past 32-bit range. The function returns a long for this reason. Use long for the score, the gain, and any intermediate difference.
How do I prepare for this in 48 hours?+
Practice the pattern of reasoning about which terms change after an operation, rather than simulating it. Write the boundary cases by hand, then the interior max/min trick. Code it once from scratch and check both examples, expecting 10 and 5.