PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates proficiency in string processing, frequency management, and algorithmic optimization for locating constrained contiguous substrings. Commonly asked in the coding & algorithms domain because it reveals how candidates reason about character distinctness and efficiency trade-offs, it targets practical application of algorithmic techniques rather than purely conceptual theory.

  • medium
  • Microsoft
  • Coding & Algorithms
  • Software Engineer

Find shortest substring with n unique letters

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

### Problem You are given a string `s` consisting of lowercase English letters and an integer `n` (1 ≤ n ≤ 26). Find the length of the shortest **contiguous** substring of `s` that contains **exactly** `n` distinct characters. If no such substring exists, return `-1`. #### Example - Input: `s = "aabcbcdbca"`, `n = 3` - One shortest substring with exactly 3 distinct characters is `"bca"` (length 3), so the output should be `3`.

Quick Answer: This question evaluates proficiency in string processing, frequency management, and algorithmic optimization for locating constrained contiguous substrings. Commonly asked in the coding & algorithms domain because it reveals how candidates reason about character distinctness and efficiency trade-offs, it targets practical application of algorithmic techniques rather than purely conceptual theory.

You are given a string `s` consisting of lowercase English letters and an integer `n` (1 ≤ n ≤ 26). Find the length of the shortest **contiguous** substring of `s` that contains **exactly** `n` distinct characters. If no such substring exists, return `-1`. **Example** - Input: `s = "aabcbcdbca"`, `n = 3` - Output: `3` (e.g. the substring `"bca"` has exactly 3 distinct characters and length 3). **Approach hint:** Use a sliding window. Expand the right edge; whenever the window holds more than `n` distinct characters, shrink from the left until it is at most `n`. Each time the window has exactly `n` distinct characters, greedily trim leading characters whose count is greater than 1 to get the tightest window ending at the current right index, and record its length.

Constraints

  • 1 ≤ n ≤ 26
  • 0 ≤ |s| ≤ 10^5
  • s consists only of lowercase English letters
  • Return -1 when no substring has exactly n distinct characters

Examples

Input: ("aabcbcdbca", 3)

Expected Output: 3

Explanation: The substring "bca" (and others like "cbd"/"dbc") has exactly 3 distinct characters with length 3, which is the shortest possible.

Input: ("aaaa", 1)

Expected Output: 1

Explanation: A single 'a' is a substring with exactly 1 distinct character, length 1.

Input: ("abc", 4)

Expected Output: -1

Explanation: The string has only 3 distinct characters, so no substring can contain exactly 4.

Input: ("", 1)

Expected Output: -1

Explanation: An empty string has no substrings, so there is none with exactly 1 distinct character.

Input: ("abcabc", 3)

Expected Output: 3

Explanation: "abc" already has exactly 3 distinct characters; no shorter substring can reach 3 distinct chars.

Input: ("aaabbbccc", 2)

Expected Output: 2

Explanation: The transition "ab" or "bc" gives exactly 2 distinct characters in length 2, the minimum possible.

Input: ("xyzzyx", 2)

Expected Output: 2

Explanation: Adjacent differing pairs like "xy", "yz", or "zy" have exactly 2 distinct characters with length 2.

Hints

  1. A sliding window over s lets you track the count of distinct characters as you move the right edge.
  2. Whenever the window contains more than n distinct characters, advance the left edge and decrement counts until you are back to at most n.
  3. When the window has exactly n distinct characters, you can still shrink it: drop leading characters whose count is greater than 1, since removing them does not reduce the distinct count.
  4. If you never reach a window with exactly n distinct characters (e.g. the string has fewer than n distinct letters), the answer is -1.
Last updated: Jun 26, 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
  • AI Coding 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

  • Return Top K Open Businesses - Microsoft (hard)
  • Implement Memory Allocation and In-Memory Records - Microsoft (medium)
  • Sort Three Categories In Place - Microsoft (medium)
  • Implement K-Means and Detect Divisible Subarrays - Microsoft (medium)
  • Retain Top K Elements - Microsoft (medium)