Minimum Length Good Subarray
Reported by candidates from Microsoft's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
A hash map of value counts is what this Microsoft OA question runs on, and the August 2026 reports say it's a plain sliding window problem dressed up. You get an array of positive integers and a number k. Find the shortest contiguous chunk holding at least k distinct values, or return -1. If you've seen a window problem before, you can do this one. If your brain locks up under a timer, StealthCoder sits invisibly on the screen as a safety net while you work through it.
The problem
Given an array arr of n positive integers and an integer k, a contiguous subarray is good if it contains at least k distinct integers. Return the minimum length of a good subarray. If no good subarray exists, return -1. Function findMinimumLengthSubarray(arr: int[], k: int) → int Examples Example 1 arr = [2,2,1,1,3] k = 3 return = 4 The full array and the subarray [2,1,1,3] each contain the three distinct integers 1, 2, and 3. The latter has length 4, and no shorter subarray contains all three values. Constraints 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^9 1 <= k <= arr.length
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is a two-pointer window with a hash map of counts. Expand the right pointer, adding arr[right] to the map. Once the map size hits k, the window is good. Record its length, then shrink from the left, decrementing counts and deleting keys that hit zero, and keep recording the minimum while the window stays good. Each element enters and leaves once, so it's O(n). The common pitfall is forgetting to delete zero-count keys, which makes the distinct count wrong. Another is using an array for counts when values go up to 10^9. Use a map. Return -1 if the best length never updated. Check k larger than the total distinct count. In the example, [2,1,1,3] gives 4. If you blank on the shrink loop during the live OA, StealthCoder can hand you the clean version.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Minimum Length Good Subarray 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. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Microsoft's OA.
Microsoft reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Minimum Length Good Subarray FAQ
What's the trick to Minimum Length Good Subarray?+
Sliding window with a hash map of counts. Grow the right edge until the map holds k distinct values, then shrink the left edge as far as you can while it still holds k. Track the smallest window length seen along the way.
How hard is this Microsoft OA question really?+
Medium. The idea is standard once you spot the window. The bugs come from the shrink step and from removing keys at zero count. Brute force over all subarrays is O(n^2) and will time out at n up to 10^5.
Why can't I use an array for counts?+
Values go up to 10^9, so an array of that size isn't workable. Use a hash map keyed by value. The map's size is your distinct count, as long as you delete entries when their count reaches zero.
When do I return -1?+
When the whole array has fewer than k distinct values. Easiest way is to initialize the best length to infinity or n+1, and if it never changes after the loop, return -1.
How do I prepare for this in 48 hours?+
Write the sliding window with a count map from scratch two or three times. Practice the shrink loop that runs while the window is still valid. Test edge cases like k equal to 1, k equal to n, and all identical values.