← Amazon Interview Insights

Amazon·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
May 2026

Summary

Amazon coding screen for a software engineer role, one algorithm question the whole time. Pretty standard sliding window stuff but I fumbled explaining my reasoning more than I'd like to admit.

Questions Asked (1)

Q1

Given an array of fruit types on trees, find the maximum number of fruits you can collect if you can only carry two distinct types of fruit at a time (you must pick from consecutive trees and stop when you hit a third type).

Algorithms & Data Structures
Author's notes

I knew it was a sliding window problem pretty quickly, the constraint of two distinct types makes that obvious.

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 values. Use a sliding window with a hash map to track fruit counts, expanding the right pointer and shrinking the left when the window exceeds two types. Return the maximum window length seen.

Pro tip: Clarify edge cases upfront (empty array, single fruit type) and mention that the algorithm runs in O(n) time and O(1) space since the map holds at most three entries. This shows attention to detail and efficiency.

1. Clarify and Restate

Confirm that you must pick from consecutive trees and can carry only two distinct fruit types. Restate the problem as finding the longest contiguous subarray with at most two distinct values.

2. Choose Data Structures

Use a hash map to count the frequency of each fruit type in the current window, and two pointers (left and right) to represent the sliding window.

3. Expand and Shrink Window

Iterate the right pointer over the array, adding fruits to the map. When the map size exceeds 2, move the left pointer forward, decrementing counts and removing fruits with zero count, until the window is valid again.

4. Track Maximum

After each expansion, update the maximum length of the valid window. Continue until the right pointer reaches the end of the array.

5. Return Result

Return the maximum length found, which represents the maximum number of fruits you can collect.

Key Points to Mention

  • Sliding window technique for contiguous subarray problems
  • Hash map to track fruit counts and distinct types
  • Time complexity O(n) and space complexity O(1) (at most 3 entries in map)
  • Edge cases: empty array, array with one or two fruit types
  • Comparison to similar problems like 'Longest Substring with At Most K Distinct Characters'
  • Correctness: window always contains at most two distinct fruits, and we track the maximum length

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