Longest Arithmetic Subarray After One Change
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Amazon reported this one in January 2026, and it looks like a plain arithmetic subarray problem until you hit the "change one element" clause. Then it reduces to something cleaner: stitching together two arithmetic runs that share a broken element in the middle. If your OA lands in the next few days, this is a dynamic programming flavored array problem, and the trick is small once you see it. Brute force will time out at 10^5 elements. StealthCoder sits invisibly as a safety net on the live OA if you blank on the stitching logic, but the idea below is learnable tonight.
The problem
You are given an integer array deviation. You may change at most one element of the array to any integer value. After making at most one change, find the maximum possible length of a contiguous subarray that forms an arithmetic progression. The changed element stays at its original index and may be used to connect the unchanged elements before it and after it into one longer arithmetic subarray. A contiguous subarray forms an arithmetic progression if the difference between every pair of consecutive elements in that subarray is the same. Function longestArithmeticSubarrayAfterOneChange(deviation: int[]) → int Examples Example 1 deviation = [8, 5, 2, 1, 100] return = 4 Change 1 to -1. The contiguous subarray [8,5,2,-1] has common difference -3, so its length is 4. Example 2 deviation = [1, 2, 3, 4, 100, 6, 7, 8, 9, 10] return = 10 Change 100 to 5. The entire array becomes an arithmetic progression with common difference 1. Constraints Constraints: 1 <= deviation.length <= 105 -109 <= deviation[i] <= 109 You may change at most one element, and the changed value may be any integer.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The real task: for each index i, compute how far an arithmetic run extends left ending at i-1 and right starting at i+1, then decide whether changing deviation[i] can bridge them. Bridging works only if deviation[i+1] - deviation[i-1] is even, because the middle value must be the integer average, and both runs must share the same difference d = (b-a)/2. Precompute left[i] as the length of the arithmetic run ending at i and right[i] as the run starting at i. Candidates per index: extend the left run by one (change i to fit it), extend the right run by one, or merge both when the differences match. Common pitfall: forgetting the parity check and the edge cases of length 1 or 2, where the answer is just n. Another one is overflow, so use 64-bit differences. Total work is O(n). If the stitching case slips under pressure, StealthCoder is the hedge during the live OA.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Longest Arithmetic Subarray After One Change 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. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Longest Arithmetic Subarray After One Change FAQ
What's the trick to Longest Arithmetic Subarray After One Change?+
Precompute the arithmetic run length ending at each index from the left and starting at each index from the right. Then for every index, test three options: extend the left run, extend the right run, or bridge both if the neighbors' gap is even and the halves match. Take the max.
How hard is this problem really?+
Medium, leaning on careful casework more than clever theory. The O(n) idea is short, but the bridging condition and edge cases trip people up. If you've done longest turbulent subarray or similar run-length problems, you'll find it manageable.
Why does the parity check matter?+
The changed value must be an integer. To bridge a[i-1] and a[i+1] with one middle value, the middle must be their average, so a[i+1] - a[i-1] must be even. The common difference is half that gap, and it must also match the neighboring run differences.
What edge cases should I test before submitting?+
Arrays of length 1 or 2 should return n, since any two elements form a progression. Test an already arithmetic array, one with a single outlier in the middle, an outlier at either end, and values near 10^9 to catch overflow in difference calculations.
How do I prepare for this in 48 hours?+
Write the left and right run arrays by hand on Example 2 until the bridging case feels obvious. Then code it once from scratch in your language, using 64-bit ints. Run a brute force on small random arrays to confirm your O(n) answer matches.