Subarrays with K Different Integers
A hard-tier problem at 66% community acceptance, tagged with Array, Hash Table, Sliding Window. Reported in interviews at Morgan Stanley and 2 others.
Subarrays with K Different Integers is a hard array problem that hits screening rounds at places like Morgan Stanley, Roblox, and Squarepoint Capital. You're given an array and a number K, and you need to count how many contiguous subarrays contain exactly K distinct integers. The obvious sliding window approach fails because you can't just track a window with exactly K elements. The acceptance rate sits around 66%, which means most candidates who attempt it don't nail the approach on first try. This is the kind of problem where the trick isn't obvious, and panic happens in a live OA. If you blank on the pattern during the assessment, StealthCoder surfaces a working solution in seconds while staying invisible to the proctor.
Companies that ask "Subarrays with K Different Integers"
Subarrays with K Different Integers 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. Made for the engineer who has done the work but might still blank with a webcam pointed at him.
Get StealthCoderThe core insight is the transform: count subarrays with exactly K distinct integers by computing at-most-K minus at-most-(K-1). You use two sliding windows, each tracking distinct element counts with a hash table. Expand the right pointer to add elements, shrink the left when you exceed K distinct values, and calculate valid subarrays at each step. The trap candidates fall into is trying to directly track a window of exactly K distinct integers, which breaks because you can't reliably move both pointers. The two-window approach forces you to think in terms of constraints rather than exact matches. Topics here are Array and Sliding Window as the core, with Hash Table for counting distinct elements and Counting for the math. If this problem lands in your live assessment and you haven't drilled the at-most trick, StealthCoder gives you the solution pattern and lets you code it clean.
Pattern tags
You know the problem.
Make sure you actually pass it.
Subarrays with K Different Integers recycles across companies for a reason. It's hard-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. Made for the engineer who has done the work but might still blank with a webcam pointed at him. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Subarrays with K Different Integers interview FAQ
Why doesn't a single sliding window work for exactly K distinct integers?+
With one window, you can't contract reliably. If you have K distinct elements and add one more, you shrink the left side. But you don't know which element to remove without scanning, and duplicates make it ambiguous. The two-window trick sidesteps this by computing constraints instead of exact matches.
Is this still asked at Morgan Stanley and other quant firms?+
Yes. Morgan Stanley, Roblox, and Squarepoint Capital all report this problem. Quant and trading firms especially like sliding window and counting problems because they test algorithmic precision and optimization. This one stays relevant.
What's the time complexity and is it acceptable?+
O(n) time because each element enters and leaves the window once. Two pointers, one hash table for counts. Space is O(K) for the hash table. For a hard problem, this is tight and expected. Most solutions won't beat this.
How does this relate to other sliding window problems I should know?+
It's the next level up from 'longest substring with K distinct' because you're counting subarrays, not finding one longest window. The at-most transformation also appears in substring problems and is a reusable pattern for exact-constraint sliding window questions.
What's the most common mistake in code during a live OA?+
Off-by-one errors when calculating the count of valid subarrays from the window. When you shrink the left pointer, you add (right - left) to the result, but candidates often miss the logic or reset counts wrong. Getting the hash table updates correct is also a common trip-up.
Want the actual problem statement? View "Subarrays with K Different Integers" on LeetCode →