PracHub
QuestionsPremiumLearningGuidesInterview PrepNEWCoaches
|Home/Coding & Algorithms/Microsoft

Find shortest substring with n unique letters

Last updated: Mar 29, 2026

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.

Related Interview Questions

  • Return Top K Open Businesses - Microsoft (hard)
  • Implement Memory Allocation and In-Memory Records - Microsoft (medium)
  • Implement K-Means and Detect Divisible Subarrays - Microsoft (medium)
  • Sort Three Categories In Place - Microsoft (medium)
  • Retain Top K Elements - Microsoft (medium)
Microsoft logo
Microsoft
Nov 3, 2025, 12:00 AM
Software Engineer
Onsite
Coding & Algorithms
4
0

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 .

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Microsoft•More Software Engineer•Microsoft Software Engineer•Microsoft Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 7,500+ 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.