Question bank › Sliding Window
Dsa Sliding Window Medium

Max Number of Fruits in Baskets

You are given an array of integers 'fruits' where each integer represents a type of fruit. You can pick fruits in baskets, but each basket can hold only two types of fruits. Your goal is to find out the maximum number of fruits you can collect. Input Format: - The first line contains an integer 'n' (1 ≤ n ≤ 10^5), the number of elements in the 'fruits' array. - The second line contains 'n' space-separated integers representing the types of fruits. Output Format: - Print a single integer which is the maximum number of fruits you can collect. Example: Input: 7 1 2 1 2 3 2 4 Output: 5 (The longest subarray with at most two types of fruits is [1, 2, 1, 2, 3] giving a length of 5.)

Key concepts

sliding_windowgreedyarray

Practise this out loud — free

Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.

🎙 Practise this question now
Part of InterviewLab's verified interview question bank. We show the prompt and concepts to practise with — never a copy-paste solution.