PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Google

Find shortest subarray with at least k distinct

Last updated: Mar 29, 2026

Quick Overview

This question evaluates proficiency in array manipulation, frequency counting, and efficient tracking of distinct values within contiguous subarrays, measuring algorithmic problem-solving and data-structure usage.

  • medium
  • Google
  • Coding & Algorithms
  • Software Engineer

Find shortest subarray with at least k distinct

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

## Problem Given an integer array `nums` and an integer `k`, find the length of the **shortest contiguous subarray** that contains **at least `k` distinct integers**. ### Task Return the minimum length of such a subarray. If no subarray contains at least `k` distinct integers, return `-1`. ### Input/Output - Input: `nums` (length `n`), integer `k` - Output: integer (minimum length or `-1`) ### Constraints (reasonable interview assumptions) - `1 <= n <= 2*10^5` - `1 <= k <= n` - `nums[i]` fits in 32-bit signed integer ### Examples - `nums = [1,2,1,3,4], k = 3` → answer `3` (e.g., `[2,1,3]`) - `nums = [1,1,1], k = 2` → answer `-1`

Quick Answer: This question evaluates proficiency in array manipulation, frequency counting, and efficient tracking of distinct values within contiguous subarrays, measuring algorithmic problem-solving and data-structure usage.

Related Interview 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)
Google logo
Google
Jan 6, 2026, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
3
0
Loading...

Problem

Given an integer array nums and an integer k, find the length of the shortest contiguous subarray that contains at least k distinct integers.

Task

Return the minimum length of such a subarray. If no subarray contains at least k distinct integers, return -1.

Input/Output

  • Input: nums (length n ), integer k
  • Output: integer (minimum length or -1 )

Constraints (reasonable interview assumptions)

  • 1 <= n <= 2*10^5
  • 1 <= k <= n
  • nums[i] fits in 32-bit signed integer

Examples

  • nums = [1,2,1,3,4], k = 3 → answer 3 (e.g., [2,1,3] )
  • nums = [1,1,1], k = 2 → answer -1

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Google•More Software Engineer•Google Software Engineer•Google Coding & Algorithms•Software Engineer Coding & Algorithms
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.