Reported September 2026
Amazonsliding window

Longest Subarray with K Distinct Values

Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Amazon OA. Under 2s to a working solution.
Founder's read

The detail that trips people here is the tie-break: if several windows share the max length, you return the one with the smallest starting index, and you return an empty array when nothing has exactly k distinct values. Amazon candidates reported this one in September 2026. The hint says dynamic-programming, but it's really a sliding window with a frequency map. Arrays go up to 100000 elements, so anything quadratic dies. Read the constraints twice, then code the window. If you freeze on the OA, StealthCoder runs invisibly on your screen and gives you a working solution as a safety net.

The problem

Given an integer array nums and an integer k, return the longest contiguous subarray containing exactly k distinct values.
If several subarrays have the same maximum length, return the one with the smallest starting index. Return an empty array when no subarray contains exactly k distinct values.

Function
longestSubarrayWithKDistinct(nums: int[], k: int) → int[]

Examples
Example 1
nums = [1,2,1,2,3]
k = 2
return = [1,2,1,2]
The prefix [1,2,1,2] has exactly two distinct values and is the longest valid subarray.
Example 2
nums = [1,2,1,3,4]
k = 3
return = [1,2,1,3]
Two length-four windows are valid. The earliest one starts at index 0.
Example 3
nums = [1,1,1]
k = 2
return = []
No subarray contains two distinct values.

Constraints
1 <= nums.length <= 100000.
-10^9 <= nums[i] <= 10^9.
1 <= k <= nums.length.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is that "exactly k" is awkward for a sliding window, but "at most k" is easy. For each right pointer, add nums[right] to a hash map of counts. While distinct count exceeds k, shrink from the left. But you need exactly k, so only record a candidate when the map size equals k. Because the window is the longest ending at right with at most k distinct, if its size is k, it's valid. Update the best only when the length is strictly greater, which keeps the smallest start index automatically since you scan left to right. The pitfall is using >= and overwriting earlier ties. Another one is forgetting the empty-array case when k distinct never appears, like [1,1,1] with k=2. Time is O(n), space is O(k). If you blank during the live OA, StealthCoder is the hedge that shows the window logic on screen without the proctor seeing it.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

You can drill Longest Subarray with K Distinct Values 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 would have shipped this the night before his JPMorgan OA if he'd had it.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Amazon's OA.

Amazon reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Longest Subarray with K Distinct Values FAQ

How hard is Longest Subarray with K Distinct Values really?+

Medium. The core is a standard sliding window with a hash map. The difficulty is the exactly-k requirement and the tie-break rule. If you've seen the at-most-k variant, you can finish this in about 15 minutes of coding.

What's the trick for the exactly k part?+

Shrink the window whenever distinct count goes above k, then only record a result when the map size is exactly k. Since the window is already maximal for that right end, you don't need the two at-most-k subtraction trick, which is for counting subarrays.

How do I handle ties on length?+

Only update your best when the new length is strictly greater than the current best. Scanning left to right means the first window found at a given max length has the smallest starting index, so later equal-length windows must not overwrite it.

Is dynamic programming actually needed?+

No. Despite the hinted pattern, a sliding window with a frequency map solves it in O(n). DP would add memory and complexity for nothing. Two pointers plus a hash map is the clean approach, and it handles 100000 elements easily.

How do I prepare for this in 48 hours?+

Write the at-most-k distinct window from scratch twice, then add the exact-k check and tie-break. Test with [1,2,1,2,3] k=2, [1,2,1,3,4] k=3, and [1,1,1] k=2. Also test k equal to the array length and negative values.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Amazon.

OA at Amazon?
Invisible during screen share
Get it