← Amazon Interview Insights

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

Intermediate
May 2026

Summary

Amazon SWE coding round with a sliding window or sorting-based problem about finding the longest subsequence where adjacent sorted elements differ by less than k. Pretty clean problem but the constraints make you think twice about brute force.

Questions Asked (1)

Q1

Given an integer array and a positive integer k, find the maximum length of a subsequence of distinct elements such that when sorted, every adjacent pair differs by less than k.

Algorithms & Data Structures
Author's notes

My first instinct was to sort the whole array and then do a sliding window pass, which works fine.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the problem and constraints, then propose sorting the array and using a sliding window to find the longest contiguous subarray with adjacent differences less than k. Since the subsequence must have distinct elements, handle duplicates by deduplication before applying the sliding window.

Pro tip: Always discuss edge cases and time/space complexity upfront; Amazon values candidates who consider scalability and real-world constraints like large inputs or duplicates.

1. Clarify requirements and constraints

Ask about input size, value ranges, and whether the subsequence must preserve original order. Confirm that 'subsequence' here means selecting elements and sorting them, so order doesn't matter.

2. Sort and deduplicate

Sort the array to bring close values together, then remove duplicates to ensure distinct elements. This simplifies the problem to finding the longest contiguous subarray where adjacent differences are less than k.

3. Apply sliding window

Use two pointers to maintain a window where the difference between the maximum and minimum elements is less than k. Since the array is sorted, this condition is equivalent to adjacent differences being less than k.

4. Track maximum length

Expand the right pointer and shrink the left pointer when the condition fails, updating the maximum window length. Return the maximum length found.

5. Analyze complexity and test

State that sorting takes O(n log n) and the sliding window is O(n), so overall O(n log n) time and O(n) space for the sorted array. Walk through edge cases like k=1, empty array, or all duplicates.

Key Points to Mention

  • Sorting the array to group close values together
  • Deduplication to ensure distinct elements
  • Sliding window technique to find the longest valid contiguous subarray
  • Time complexity O(n log n) due to sorting, space complexity O(n)
  • Edge cases: k=1, empty array, all elements identical, large k
  • Proof that the longest valid subsequence corresponds to a contiguous subarray after sorting and deduplication

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