PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates understanding of array algorithms, handling of distinct-element counting, and the ability to design time- and space-efficient solutions for constrained subarray queries.

  • medium
  • Google
  • Coding & Algorithms
  • Software Engineer

Find shortest subarray with ≥k distinct integers

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Given an integer array `nums` of length `n` and an integer `k`, find the length of the **shortest contiguous subarray** that contains **at least `k` distinct** integers. If no subarray contains at least `k` distinct integers, return `-1`. ### Input - `nums`: array of integers - `k`: integer ### Output - Integer: minimum length, or `-1` ### Constraints (typical) - `1 <= n <= 2*10^5` - `1 <= k <= n` - `|nums[i]| <= 10^9` (An efficient solution is expected; quadratic approaches may time out.)

Quick Answer: This question evaluates understanding of array algorithms, handling of distinct-element counting, and the ability to design time- and space-efficient solutions for constrained subarray queries.

Given an integer array `nums` and an integer `k`, return the length of the shortest contiguous subarray that contains at least `k` distinct integers. A subarray is contiguous, and values are considered distinct by their integer value. If no such subarray exists, return `-1`.

Constraints

  • 1 <= len(nums) <= 2 * 10^5
  • 1 <= k <= len(nums)
  • -10^9 <= nums[i] <= 10^9

Examples

Input: ([1, 2, 1, 3, 4], 3)

Expected Output: 3

Explanation: The shortest valid subarrays are [2, 1, 3] and [1, 3, 4], both of length 3.

Input: ([1, 1, 1], 2)

Expected Output: -1

Explanation: There is only 1 distinct integer in the entire array, so reaching 2 distinct integers is impossible.

Input: ([5, 5, 5, 5], 1)

Expected Output: 1

Explanation: Any single-element subarray already contains at least 1 distinct integer.

Input: ([], 1)

Expected Output: -1

Explanation: An empty array has no non-empty subarray, so the requirement cannot be met.

Input: ([1, 2, 2, 3, 1], 2)

Expected Output: 2

Explanation: A shortest valid subarray is [1, 2] or [2, 3], both with 2 distinct integers and length 2.

Input: ([4, -1, 4, -1, 2], 3)

Expected Output: 3

Explanation: The subarray [4, -1, 2] contains 3 distinct integers and has length 3, which is minimal.

Input: ([7, 8], 0)

Expected Output: 0

Explanation: With k = 0, the empty subarray already satisfies the condition.

Hints

  1. Use a sliding window and keep track of the frequency of each value inside the current window.
  2. Whenever the window has at least `k` distinct values, try shrinking it from the left to make it as short as possible.
Last updated: Apr 22, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Solve Rooms and Top-K Streams - Google (medium)
  • Find Containing Range - Google (medium)
  • Rearrange Tasks With Cooldown - Google (medium)
  • Implement Employee Management and Expression Evaluation - Google (medium)
  • Solve Three Array and Matrix Path Problems - Google (medium)