Find Subarray with Minimum Distinct Integers
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Amazon threw this at candidates in September 2024, and it's a sliding window problem disguised as a subarray hunt. You're looking for the shortest contiguous subarray that contains at least K distinct integers (or exactly K, depending on the exact wording you see). The trick is that you don't precompute anything. You build a window on the fly, shrink it when you've hit your target, and track the minimum length. StealthCoder will spot the pattern instantly if you blank on the implementation.
Pattern and pitfall
The core move is a two-pointer sliding window with a hash map tracking character frequencies. Expand the right pointer until you hit K distinct integers, then contract the left pointer while the condition holds, updating your minimum length each time. Most candidates mess this up by either not shrinking aggressively enough or forgetting to count distinct integers correctly. The hash map is your source of truth for distinctness, not the window itself. When you shrink and a character count drops to zero, decrement your distinct count. StealthCoder catches this logic in real time during the OA, so you don't have to hold the whole state machine in your head.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Find Subarray with Minimum Distinct Integers cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as subarrays with k different integers. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Find Subarray with Minimum Distinct Integers FAQ
Is this asking for exactly K distinct or at least K distinct integers?+
The title says 'minimum distinct,' which usually means 'at least K.' If the problem statement says 'exactly K,' the approach is the same but you need two sliding windows or a helper function. Read the problem text carefully on the OA itself.
Do I need to return the subarray or just the length?+
Track the minimum length and the start index. You can reconstruct the subarray if asked, but most versions just want the integer length. Store both to be safe.
What's the most common mistake on this problem?+
Forgetting to decrement the distinct count when a character frequency hits zero during the shrink phase. Your hash map can have stale entries. Clean them up or check count > 0 before trusting the size.
Can I solve this in one pass?+
Yes. Expand right, shrink left, track minimum as you go. No preprocessing needed. Time is O(n), space is O(k) where k is the alphabet size (usually 26 for lowercase letters).
Is this still asked at Amazon in 2024?+
Yes. It was reported in September 2024. It's a core sliding window pattern Amazon loves. If you see 'subarray' and 'distinct' together, immediately think two pointers with a frequency map.