← Amazon Interview Insights

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

Intermediate
Jun 2026

Summary

Amazon SWE online assessment with a sliding window problem. Pretty standard stuff if you've done any leetcode prep, but the edge cases kept me second-guessing myself.

Questions Asked (1)

Q1

Given an array of fruit types, find the maximum length of a contiguous subarray containing at most two distinct values. You have two baskets, each holding one fruit type with unlimited capacity.

Algorithms & Data Structures
Author's notes

Classic sliding window problem once you see it, but I spent way too long overthinking the data structure for tracking counts.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Recognize this as the 'Fruit Into Baskets' problem, which is equivalent to finding the longest subarray with at most two distinct elements. Use a sliding window with a hash map to track fruit counts, expanding the right pointer and shrinking the left when distinct types exceed two. Maintain the maximum window length throughout.

Pro tip: Clarify that the two baskets represent at most two distinct fruit types, and emphasize that the sliding window approach achieves O(n) time and O(1) space since the map holds at most three entries. This shows you understand the problem's constraints and can optimize accordingly.

1. Clarify the problem

Confirm that you need the longest contiguous subarray with at most two distinct values, and that each basket holds one type. Ask about edge cases like empty input or fewer than two types.

2. Choose the sliding window technique

Explain that a sliding window with two pointers (left and right) efficiently tracks a valid subarray. Use a hash map to count occurrences of each fruit type in the current window.

3. Expand and shrink the window

Move the right pointer to include a new fruit, updating the map. If the number of distinct fruits exceeds two, move the left pointer forward, decrementing counts and removing fruits with zero count, until the window is valid again.

4. Track the maximum length

After each expansion, update the maximum length with the current window size (right - left + 1). Continue until the right pointer reaches the end of the array.

5. Analyze complexity and test

State that time complexity is O(n) because each element is visited at most twice, and space is O(1) since the map holds at most three entries. Walk through a small example to verify correctness.

Key Points to Mention

  • Sliding window pattern for contiguous subarray problems
  • Hash map to track fruit counts and distinct types
  • Two-pointer technique with left and right boundaries
  • Time complexity O(n) and space complexity O(1)
  • Handling edge cases: empty array, single fruit type, all same fruit
  • Relationship to 'Fruit Into Baskets' problem (LeetCode 904)

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