As reported: American Express Engineer II, round 1 (technical, two interviewers, 2025): Fruit Into Baskets, a sliding-window question.
You walk along a row of fruit trees; fruits[i] is the type of fruit tree i produces. You have two baskets, and each basket can hold only one type of fruit (any amount of it). Starting from any tree, you pick exactly one fruit from every tree while moving right, and you must stop at the first tree whose fruit fits in neither basket.
Return the maximum number of fruits you can pick.
This is LeetCode 904 — Fruit Into Baskets.
Example 1
fruits = [1,2,1] -> 3
Example 2
fruits = [0,1,2,2] -> 3 start at tree 1: [1,2,2]
Example 3
fruits = [1,2,3,2,2] -> 4 start at tree 1: [2,3,2,2]
Example 4
fruits = [3,3,3,1,2,1,1,2,3,3,4] -> 5 [1,2,1,1,2]
Constraints: 1 ≤ fruits.length ≤ 10^5; 0 ≤ fruits[i] < fruits.length.
Code runner format: one line: fruits as a JSON array. Implement totalFruit(fruits); the harness prints the returned number.