Fruit Into Baskets
Medium · Arrays & Strings · about 15 minutes
Asked at Amazon, Google, Meta, Bloomberg, ByteDance, Salesforce, ServiceNow, Workday, Rippling.
A row of trees each bears one kind of fruit: tree i bears kind fruits[i]. You carry two baskets, and each basket may hold any amount of a single kind. Begin at any tree and walk right, taking one fruit from each tree; you have to stop at the first tree whose kind fits neither basket. What is the most fruit you can collect?