Shortest Substring with at Least K Distinct Characters
Updated
Problem statement
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) → intExamples
Example 1
text = "aabcbcdbca"k = 3return = 3The substring abc has three distinct characters.
Example 2
text = "aaaa"k = 2return = -1Only one distinct character exists.
Constraints
1 <= text.length <= 200000.1 <= k <= 256.- For this exercise, assume
textcontains 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