Maximum Valid Substring Frequency
Reported by candidates from Microsoft's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Microsoft reportedly served this one in October 2026, and the input size is the first thing to read. The text runs up to 10^5 characters, so checking every substring from scratch is dead on arrival. You're counting how often a substring repeats while it stays under a distinct-letter cap and inside a length range. It's tagged sliding window, and that's the right instinct. If your brain freezes when the OA clock starts, StealthCoder runs invisibly on your desktop as a safety net. Know the trick first, though.
The problem
Given a lowercase string text and three integers maxDistinct, minLength, and maxLength, return the maximum number of occurrences of any substring that satisfies both rules: Its length is between minLength and maxLength, inclusive. It contains at most maxDistinct distinct letters. Occurrences may overlap. Return 0 when no substring satisfies the rules. Function maxSubstringFrequency(text: String, maxDistinct: int, minLength: int, maxLength: int) → int Examples Example 1 text = "aababcaab" maxDistinct = 2 minLength = 3 maxLength = 4 return = 2 The substring aab appears twice, and it contains only the letters a and b. Example 2 text = "aaaa" maxDistinct = 1 minLength = 3 maxLength = 3 return = 2 The two length-three windows are both aaa, so overlapping occurrences produce frequency 2. Example 3 text = "abcde" maxDistinct = 2 minLength = 3 maxLength = 3 return = 0 Every length-three substring contains three distinct letters, so none is valid. Constraints 1 <= text.length() <= 10^5. text contains only lowercase English letters. 1 <= maxDistinct <= 26. 1 <= minLength <= maxLength <= min(26, text.length()).
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is in the constraints: maxLength is at most 26. That means you only have 26 possible window lengths, and 26 times 10^5 is tiny. Better still, a longer valid substring always contains a shorter valid one at the same start, with at most as many distinct letters. And any substring's frequency is capped by its prefix of minLength. So you only need to check windows of length minLength. Slide one window across the text, track letter counts and distinct count, and when distinct is at most maxDistinct, increment a hash map of that substring. Return the max count, or 0. The pitfall is building substrings with heavy slicing in a loop, or forgetting overlaps count. Example 2 proves overlaps matter. If you blank on why only minLength matters, StealthCoder is the hedge during the live OA, quietly handing you the solution.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Maximum Valid Substring Frequency 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. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as maximum number of occurrences of a substring. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Microsoft's OA.
Microsoft reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Maximum Valid Substring Frequency FAQ
What's the trick in Maximum Valid Substring Frequency?+
Only windows of length minLength matter. Any longer valid substring contains a valid minLength substring that appears at least as often. So slide a fixed window of minLength, keep letter counts, and count valid substrings in a hash map.
How hard is this Microsoft OA question really?+
Medium. The sliding window is easy, but the insight that you can ignore maxLength is what separates a pass from a timeout. Once you see it, the code is about 20 lines. Without it, you'll try multiple window sizes and waste time.
Do overlapping occurrences count?+
Yes. Example 2 shows it: in aaaa with length 3, the windows at index 0 and 1 are both aaa, so the frequency is 2. A fixed sliding window naturally counts overlaps, so don't skip ahead by the window length.
What's the time complexity I should aim for?+
O(n * minLength) if you build each substring key from the window, which is fine since minLength is at most 26. The distinct count updates in O(1) per slide using a 26-size count array. Space is bounded by the number of distinct substrings stored.
How do I prepare for this in 48 hours?+
Write the fixed-size window with a count array and a distinct counter until it's automatic. Then practice the map-of-substrings counting step. Test your code on the three examples, especially the zero-result case where no window is valid.