← Amazon Interview Insights

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

Intermediate
May 2026

Summary

Amazon SWE coding round, one problem the whole time. Pretty standard sliding window / prefix sum territory but the O(n) constraint is where things get interesting.

Questions Asked (1)

Q1

Given an integer array and a target sum, find the length of the longest contiguous subarray that sums to the target. Must run in O(n) time. Return 0 if none exists.

Algorithms & Data Structures
Author's notes

The naive approach is obvious and wrong for the time constraint.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a hash map to store the earliest index where each prefix sum occurs, then iterate through the array computing the running sum. For each index, check if (current sum - target) exists in the map; if so, update the maximum length. This yields O(n) time and O(n) space.

Pro tip: Clarify upfront that the array can contain negative numbers, which rules out sliding window and necessitates the prefix sum approach. Also, mention that you store the earliest index to maximize subarray length.

1. Clarify constraints and edge cases

Ask about array size, possible values (negative, zero), and whether the subarray must be non-empty. Confirm that O(n) time is required and O(n) space is acceptable.

2. Explain the prefix sum concept

Define prefix sum at index i as the sum of elements from 0 to i. The sum of subarray (i, j] is prefix[j] - prefix[i], so we need prefix[j] - prefix[i] = target.

3. Design the hash map strategy

Use a hash map to store the first occurrence of each prefix sum. Initialize with {0: -1} to handle subarrays starting at index 0. Iterate through the array, updating the running sum and checking if (sum - target) is in the map.

4. Update maximum length and map

If (sum - target) exists, compute the subarray length as current index minus the stored index, and update the maximum. If the current sum is not in the map, add it with the current index to preserve the earliest occurrence.

5. Analyze complexity and test

State that time complexity is O(n) and space is O(n). Walk through a small example, including a case with negative numbers and a case with no valid subarray, to verify correctness.

Key Points to Mention

  • Prefix sum technique and its relation to subarray sums
  • Hash map storing earliest index for each prefix sum
  • Handling negative numbers (why sliding window fails)
  • Initialization with {0: -1} to cover subarrays starting at index 0
  • Time and space complexity analysis (O(n) time, O(n) space)
  • Edge cases: empty array, no valid subarray, target zero

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