Fruit Into Baskets
A medium-tier problem at 46% community acceptance, tagged with Array, Hash Table, Sliding Window. Reported in interviews at Deutsche Bank and 2 others.
Fruit Into Baskets is a deceptive sliding-window problem that Google, Deutsche Bank, and Walmart Labs have all asked. You're given an array of fruit types and asked to pick the longest contiguous subarray containing at most two distinct types. The catch: the obvious brute-force scan times out. It looks straightforward until your O(n²) solution hits the wall in the live OA. The real solution uses a two-pointer sliding window with a hash table to track fruit counts, collapsing in O(n) time. If the pattern doesn't click, StealthCoder runs invisibly during your assessment and surfaces the working approach when you're stuck.
Companies that ask "Fruit Into Baskets"
Fruit Into Baskets is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by a senior engineer who knows the OA is theater. This is the script.
Get StealthCoderThe trap is thinking this is just a 'find longest subarray' problem. Candidates often try nested loops or greedy picks, which fail on tricky test cases with clustered fruit types. The actual pattern: maintain two pointers and slide right, adding fruits to a hash map. When you exceed two fruit types, shrink from the left until you're back to exactly two. The hash table tracks counts, not just presence. Most people miss that you need to erase a key when its count hits zero, or the window never actually contracts. This is fundamentally a hash table and sliding window problem dressed as an array question. When you hit the live OA and aren't sure if your window logic is right, StealthCoder surfaces a complete, tested solution in seconds.
Pattern tags
You know the problem.
Make sure you actually pass it.
Fruit Into Baskets recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by a senior engineer who knows the OA is theater. This is the script. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Fruit Into Baskets interview FAQ
Is this really a Google-level problem?+
Yes. Google, Deutsche Bank, and Walmart Labs have all asked it. The 46% acceptance rate reflects that many candidates don't nail the sliding-window mechanics under pressure. It's medium difficulty but often feels harder because the pattern isn't obvious.
What's the trick I'm supposed to know?+
Don't iterate twice. Use a single pass with two pointers and a hash table. Slide the right pointer in, add fruits. When you hit more than two types, slide the left pointer out until you're back to two. The hash table counts, not just existence.
Why does my greedy approach fail?+
Greedy picks don't work because you can't always know in advance which fruits to keep. You have to explore the window and backtrack when you exceed the constraint. That's what sliding window does for you.
How does this relate to other sliding-window problems?+
It's a direct cousin of 'Longest Substring Without Repeating Characters.' Both fix the window constraint (max 2 distinct here, max 1 of each there) and shrink left when the constraint breaks. Hash table tracks cardinality both times.
What's the most common bug in the live OA?+
Forgetting to decrement the count when you move the left pointer, or failing to erase a key when its count reaches zero. This breaks the window logic and produces wrong answers on cases with many consecutive same-type fruits.
Want the actual problem statement? View "Fruit Into Baskets" on LeetCode →