DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
Back to Arena
medium
Sliding Window

Fruit Into Baskets

**Problem Statement:** You are visiting a farm that has a single row of fruit trees arranged from left to right. The trees are represented by an integer array `fruits` where `fruits[i]` is the type of fruit the `i`th tree produces. You only have two baskets, and each basket can only hold a single type of fruit. Return the maximum number of fruits you can pick. **Hint / Expected Approach:** At-most-2-distinct sliding window **Edge Cases to Consider:** - (1) Only one type of fruit - (2) All distinct types - (3) Length 1 array

Examples

Example 1
Input: [1,2,1]
Output: 3
Example 2
Input: [0,1,2,2]
Output: 3
Example 3
Input: [1,2,3,2,2]
Output: 4

Constraints

  • ▪1 <= data.length <= 10^5
  • ▪Memory complexity must be O(1) or O(k)
  • ▪Ensure non-overlapping pointer access

Watch Out For Edge Cases

  • ▪Only one type of fruit
  • ▪All distinct types
  • ▪Length 1 array
Frequently Asked At
GoogleAmazonAdobe