Minimum Length Good Subarray
Updated
Problem statement
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) → intExamples
Example 1
arr = [2,2,1,1,3]k = 3return = 4The 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^51 <= arr[i] <= 10^91 <= k <= arr.length
More Microsoft Online Assessment questions
- Authentication SystemOA · Seen Nov 2026
- Maximum String OperationsOA · Seen Nov 2026
- Minimum Cost to Process Priority TasksOA · Seen Nov 2026
- Most Visited Sprint MarkerOA · Seen Nov 2026
- Maximum Escape Game ScoreOA · Seen Nov 2026
- Count Subarrays with a Perfect-Square ProductOA · Seen Nov 2026
- Maximum Interactive Team SizeOA · Seen Nov 2026
- Maximize Pipeline Throughput Under a Scaling BudgetOA · Seen Nov 2026
- Minimum Cost K-Capable ModelsOA · Seen Nov 2026
- Find Available Meeting RoomsOA · Seen Oct 2026
Similar questions at other companies
- Count Belt Sections With K Fruit Pairs (Forward Deployed Engineer)Boston Consulting Group · OABoston Consulting Group interview questions
- Count Servers With No Requests in a Time WindowIBM · OAIBM interview questions
- Count Subarrays with at Least K Equal-Fruit PairsTikTok · OATikTok interview questions
- Consistent LogsRippling · OARippling interview questions
- Auction Sniping Window MinimumCisco · OACisco interview questions
- Balanced Prefix Sets in a PermutationUber · OAUber interview questions
- Shortest Subarray Covering an Integer RangeMarshall Wace · OAMarshall Wace interview questions
- Count Subarrays with at Least K Distinct ValuesVisa · OAVisa interview questions
- Longest Frequency-Bounded SubarrayZipRecruiter · OAZipRecruiter interview questions
- Find Subarray With Minimum Distinct IntegersAmazon · OA
Microsoft interview experiences
- Microsoft OA Questions, October 2026: 4 Experiences
- Microsoft Experienced SWE Phone Screen: 1 Experience
- Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
- Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
- Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
- Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)