← Amazon Interview Insights

Amazon·Software Engineer·Online Assessment (OA)·Intermediate

Intermediate
May 2026

Summary

Amazon OA for a SWE role, one algorithmic problem about subsequences and circular arrays. Pretty niche constraint setup, took me a while to even understand what they were asking.

Questions Asked (1)

Q1

Given an array of integers, find the longest subsequence that can be rearranged into a circular array where the absolute difference between every pair of adjacent elements (including the wrap-around from last to first) is at most 1.

Algorithms & Data Structures
Author's notes

The circular part is what got me.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, recognize that the condition implies the subsequence can only contain values from a contiguous range of at most two distinct integers (x and x+1). Then, for each possible pair (x, x+1), count the frequencies of x and x+1 in the array, and determine the maximum length of a valid circular arrangement using those counts. The answer is the maximum over all such pairs.

Pro tip: Clarify the definition of 'subsequence' upfront—if it means a subset of indices (order irrelevant), the problem reduces to frequency counting; if it means a contiguous subarray, the approach changes. Also, explicitly handle edge cases like empty array, single element, and all elements equal.

1. Clarify the problem

Confirm that 'subsequence' means any subset of elements (order can be rearranged) and that the circular condition applies to the rearranged sequence. Ask if the subsequence must be contiguous in the original array.

2. Identify the key constraint

Observe that for all adjacent differences to be ≤1 in a circular arrangement, the set of values in the subsequence must be a subset of {x, x+1} for some integer x. Thus, only two consecutive values can appear.

3. Count frequencies

For each possible pair (x, x+1), count how many times x and x+1 appear in the array. Let these counts be a and b.

4. Compute maximum valid length

For a given pair (a, b), the maximum length of a valid circular arrangement is: if a == b, then 2a; if a > b, then 2b + 1; if b > a, then 2a + 1. (This ensures the circular sequence alternates as much as possible.)

5. Iterate and return maximum

Iterate over all possible x (from min to max in the array), compute the valid length for each pair, and return the overall maximum. Handle edge cases (empty array, single element) separately.

Key Points to Mention

  • The subsequence can only contain at most two distinct values that differ by 1.
  • The circular arrangement requires that the counts of the two values differ by at most 1 (except when one count is zero).
  • The maximum length for counts a and b is 2*min(a,b) + (1 if a != b else 0).
  • Time complexity: O(n) with a hash map to count frequencies, then O(k) to check pairs, where k is the number of distinct values.
  • Edge cases: empty array returns 0; single element returns 1; all elements equal returns n.
  • The problem is equivalent to finding the longest 'almost alternating' sequence of two consecutive integers.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.