FastPrepLongest Arithmetic Subarray After One Change
Problems/Longest Arithmetic Subarray After One Change

Longest Arithmetic Subarray After One Change

Amazon logoAmazonFULLTIMEOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

You are given an integer array deviation.

You may change at most one element to any integer you like, or change nothing. After that, find the longest subarray that forms an arithmetic progression, and return its length.

The changed element stays in its place. It can link the unchanged elements on its left and on its right into one longer arithmetic subarray.

A subarray is a run of elements that sit next to each other in the array. It forms an arithmetic progression when each element minus the one just before it gives the same value all the way through.

Function

longestArithmeticSubarrayAfterOneChange(deviation: int[]) → int

Examples

Example 1

deviation = [8, 5, 2, 1, 100]return = 4

Change the 1 to -1. The start of the array becomes [8,5,2,-1], and each step in it is -3. That run is arithmetic, so the answer is its length, 4.

Example 2

deviation = [1, 2, 3, 4, 100, 6, 7, 8, 9, 10]return = 10

Change the 100 to 5. Now the whole array goes up by 1 at every step, so the answer is the length of the whole array.

Constraints

  • 1 <= deviation.length <= 105
  • -109 <= deviation[i] <= 109
  • You may change at most one element, and the new value can be any integer.

More Amazon Online Assessment questions

  • Maximize K-Element AND with an Increment BudgetOA · Seen Nov 2026
  • Task SchedulerOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Top K Frequent ElementsOA · PHONE SCREEN · Seen Nov 2026
  • Get Smallest Base SegmentOA · Seen Nov 2026
  • Minimum Cost of Left and Right PropagationOA · Seen Nov 2026
  • Minimum Grid InconvenienceOA · Seen Nov 2026
  • Maximize Shipment Order by Sorting One WindowOA · Seen Nov 2026
  • Maximum Equal-Bit Pairs in a SubsequenceOA · Seen Nov 2026
  • First Non-Repeating CharacterOA · ONSITE INTERVIEW · Seen Nov 2026
  • Two SumOA · PHONE SCREEN · Seen Nov 2026

Interview reports that mention this question

  • Amazon Full-Time OA Problems, Recent Reports (Part 4 of 5)

Amazon interview experiences

  • Amazon Experienced SDE Onsite Questions: 1 Experience
  • Amazon SDE Onsite Interview Questions: 9 Experiences
  • Amazon SDE II and L5 FDE Interview Questions: 4 Experiences
  • Amazon Full-Time OA Problems, Earlier Reports (Part 5 of 5)
  • Amazon New Grad OA Problems: Every Task We Have (Part 3 of 5)
See Amazon hiring insights
Saved
class Solution {
    public int longestArithmeticSubarrayAfterOneChange(int[] deviation) {
      // write your code here
    }
}
Checking account…