Shortest Substring with at Least K Distinct Characters
Reported by candidates from Navan's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The edge case that kills the naive answer on this Navan OA, reported in January 2024, is the one where k is bigger than the number of distinct characters in the whole string. Skip that check and you return garbage instead of -1. The task is simple to state: find the shortest contiguous substring with at least k distinct characters. It's a sliding window problem on a string up to 200000 long, so brute force over all substrings won't survive. If you blank during the live OA, StealthCoder runs invisibly as a safety net and hands you the window logic. Know the shape first, though.
The problem
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 = 3 return = 3 The substring abc has three distinct characters. Example 2 text = "aaaa" k = 2 return = -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.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is a two-pointer window with a frequency array of size 256. Expand the right pointer, count a new distinct character when its count goes from 0 to 1. Once distinct >= k, shrink from the left as long as the condition still holds, and record the window length at each valid step. Shrinking is safe because dropping a character only matters when its count hits 0, which lowers distinct. The pitfalls are real. Forgetting to return -1 when no window ever qualifies. Updating the answer only after the shrink loop instead of during it. Using a hash map when a fixed 256 array is faster and simpler. Also remember k can be 1, so the answer is 1 for any nonempty text. Total work is O(n) since each pointer moves at most n times. If the live OA freezes you, StealthCoder is the hedge that gets the template on screen.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Shortest Substring with at Least K Distinct Characters cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Navan's OA.
Navan reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Shortest Substring with at Least K Distinct Characters FAQ
What's the trick for Shortest Substring with at Least K Distinct Characters?+
Use a sliding window with a count array. Grow the right end until you have k distinct characters, then shrink the left end while you still have k. Record the length each time the window is valid. It's O(n) time and O(256) space.
How hard is this Navan OA question really?+
Medium at most. It's a standard variable-size sliding window. The difficulty is in the details: correct distinct counting, shrinking while valid, and returning -1. If you've written one window problem before, you can do this.
What edge cases should I test?+
Test k larger than the distinct count, which returns -1. Test k equal to 1, which returns 1. Test a string of all the same character, and a string where the best window is at the very end. Also test text of length 1.
Why not check every substring?+
With length up to 200000, checking all substrings is O(n^2) or worse and will time out. The window works because extending right never loses distinct characters, so each pointer only moves forward once.
How do I prepare for this in 48 hours?+
Write the window template from memory twice, once for at most K distinct and once for at least K distinct. Run the two examples by hand. Then practice the shrink-while-valid loop until it's automatic, since that's where most bugs show up.