Find minimum subarray length with k distinct integers
Company: IBM
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question tests proficiency with the sliding window technique and hash map-based frequency tracking, applied to subarray problems requiring exact cardinality constraints. It assesses the ability to distinguish between "exactly k distinct" and "at most k distinct" variants — a nuance that reveals depth in algorithmic thinking commonly evaluated in software engineering interviews.
Constraints
- 0 <= len(arr) <= 10^5
- Array values may be any integers (positive, negative, or zero)
- k may be 0 or larger than len(arr); return -1 when no qualifying subarray exists
- Target O(n) time and O(n) extra space
Examples
Input: ([1, 2, 1, 2, 3], 2)
Expected Output: 2
Explanation: Windows like [1,2], [2,1], [1,2] (after sliding off the leading duplicate), and finally [2,3] all hold exactly 2 distinct values; the minimum length is 2.
Input: ([1, 1, 1, 1], 2)
Expected Output: -1
Explanation: Only one distinct value exists, so no window ever reaches 2 distinct values.
Input: ([1, 2, 3, 4, 5], 3)
Expected Output: 3
Explanation: All values are unique, so the shortest window with exactly 3 distinct values is any 3 consecutive elements, length 3.
Input: ([5, 5, 5], 1)
Expected Output: 1
Explanation: A single element already has exactly 1 distinct value; trimming duplicates yields length 1.
Input: ([], 1)
Expected Output: -1
Explanation: Empty array — the loop never runs, so no qualifying window exists.
Input: ([7, 7, 7, 7], 0)
Expected Output: -1
Explanation: k = 0 is invalid: any non-empty window has at least one distinct value, so return -1 up front.
Input: ([1, 2, 2, 2, 1, 3], 2)
Expected Output: 2
Explanation: The window [1,2] at the start already has exactly 2 distinct values, length 2.
Input: ([-1, 0, -1, 2, 0], 2)
Expected Output: 2
Explanation: Negative and zero values are handled by equality; the window [-1,0] (and others) gives exactly 2 distinct values, length 2.
Input: ([4, 3, 5, 2, 1], 7)
Expected Output: -1
Explanation: k exceeds the array's distinct count (5), so distinct never reaches 7 — return -1.
Input: ([1, 2, 3, 2, 2, 2, 1], 3)
Expected Output: 3
Explanation: The window [1,2,3] at the start holds all three distinct values {1,2,3}, length 3; no shorter window contains all three.
Input: ([10], 1)
Expected Output: 1
Explanation: A single-element array with k = 1 gives a length-1 window.
Input: ([3, 1, 1, 1, 1, 2], 3)
Expected Output: 6
Explanation: The only value 3 is at index 0 and the only value 2 is at index 5, so the only window containing all of {3,1,2} spans the entire array, length 6.
Hints
- Use a single left-to-right sliding window with a hash map from value -> count inside the window. The number of map keys with positive count is the current distinct count.
- 'At least k distinct' is easy; for 'exactly k', once the window holds k distinct values keep shrinking from the left as long as the leftmost element is a duplicate (its in-window count stays >= 1 after removal). Stop the moment removing it would drop a distinct value (count would hit 0), then record the length.
- If adding a new element makes distinct exceed k, that window can never qualify by extending right — advance left until distinct <= k again. Only measure length while distinct == k.