FastPrepShortest Substring with at Least K Distinct Characters
Problems/Shortest Substring with at Least K Distinct Characters

Shortest Substring with at Least K Distinct Characters

Navan logoNavan● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Navan onsite interview for a full-time role. Our version follows the reported exercise. The report leaves out some input limits and interface details, and we filled those in.

Our version is about 95% the same as the reported question.

Return the minimum length of a contiguous substring of text that contains at least k distinct characters. Return -1 if no such substring exists.

Function

shortestAtLeastKDistinct(text: String, k: int) → int

Examples

Example 1

text = "aabcbcdbca"k = 3return = 3

The substring abc has three distinct characters.

Example 2

text = "aaaa"k = 2return = -1

Only one distinct character exists.

Constraints

  • 1 <= text.length <= 200000.
  • 1 <= k <= 256.
  • For this exercise, assume text contains only ASCII characters; one character is one byte.

More Navan interview questions

  • Time Based Key-Value StoreONSITE INTERVIEW · Seen Dec 2025
  • Type-Ahead Prefix MatchesONSITE INTERVIEW · Seen Dec 2024
  • Minimum Steps on a Circular Character DialONSITE INTERVIEW · Seen Jan 2024
  • Minimum Stick Connection CostONSITE INTERVIEW · Seen Jan 2024
  • Compare N-Ary Tree Leaf SequencesONSITE INTERVIEW · Seen Nov 2023
  • Roman to IntegerPHONE SCREEN · Seen Oct 2024
  • Sort ColorsPHONE SCREEN · Seen Oct 2024
  • Basic CalculatorPHONE SCREEN · Seen Jun 2024
  • LRU CachePHONE SCREEN · Seen Jun 2024
  • House RobberPHONE SCREEN · Seen May 2024

Similar questions at other companies

  • Longest Substring Without Repeating CharactersOracle · ONSITE INTERVIEWOracle interview questions
  • Longest Substring Without Repeating CharactersSalesforce · ONSITE INTERVIEWSalesforce interview questions
  • Longest Substring Without Repeating CharactersMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Longest Substring Without Repeating CharactersGoogle · ONSITE INTERVIEWGoogle interview questions
  • Longest Substring Without Repeating CharactersAmazon · ONSITE INTERVIEW
  • Longest Substring Without Repeating CharactersInfosys · ONSITE INTERVIEWInfosys interview questions
  • Longest Substring Without Repeating CharactersOmnissa · ONSITE INTERVIEWOmnissa interview questions
  • Longest Substring Without Repeating CharactersBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
  • Longest Substring with At Most K Distinct CharactersGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
  • Longest Substring Without Repeating CharactersBank of America · ONSITE INTERVIEWBank of America interview questions
All Navan interview questionsSee Navan hiring insights
Saved
class Solution {
    public int shortestAtLeastKDistinct(String text, int k) {
        // Write your code here.
    }
}
Checking account…