Shortest Subarray with at Least K Distinct Values
Company: Squarepoint
Role: Quantitative Researcher
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: Find the shortest contiguous subarray containing at least k distinct values. Practice sliding-window invariants, frequency maps, and linear-time minimization.
Read the full Squarepoint Quantitative Researcher interview experience this question came from
Constraints
- 1 <= nums.length <= 200000
- 1 <= nums[i] <= 1000000000
- 1 <= k <= 200000
Examples
Input: ([1, 2, 2, 3, 1], 3)
Expected Output: 3
Explanation: The source example's shortest qualifying window is [2, 3, 1].
Input: ([5, 5, 5], 2)
Expected Output: -1
Explanation: Only one distinct value exists, so no good subarray exists.
Hints
- Track the frequency of each value in the current contiguous window.
- Once the window has at least k distinct values, move its left boundary while it remains good.