Longest Repeating Character Replacement
Reported by candidates from Microsoft's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Microsoft reported this one in September 2026, and the input size is the whole story. With s up to 100000 characters, checking every substring and counting replacements is O(n^2) at best, and it dies. If your OA invite lands in the next couple of days, this is a sliding window problem wearing a DP hint. You've seen the shape before: longest substring, at most k changes, one repeated letter. The window grows, the window slides, and you never rescan. StealthCoder sits invisible on your screen as a safety net if your mind goes blank mid-assessment, but the idea is short enough to own tonight.
The problem
You are given a string s containing only uppercase English letters and an integer k. You may replace at most k characters in s with any other uppercase English letter. Return the length of the longest substring that can contain only one repeated letter after at most k replacements. Function characterReplacement(s: String, k: int) → int Examples Example 1 s = "ABAB" k = 2 return = 4 Replace both occurrences of A with B, or both occurrences of B with A. The entire string then contains one repeated letter. Example 2 s = "AABABBA" k = 1 return = 4 The substring AABA can become AAAA with one replacement, so a length of 4 is possible. Constraints 1 <= s.length <= 100000 s contains only uppercase English letters. 0 <= k <= s.length
Reported by candidates. Source: FastPrep
Pattern and pitfall
Keep a window [left, right] and an array of 26 counts. Track maxFreq, the highest count of any single letter seen in the current window. The window is valid when its length minus maxFreq is at most k, because that difference is how many characters you'd have to replace. When it goes over k, move left forward by one and decrement that letter's count. The trick: you never need to shrink maxFreq when the window slides. The answer only improves when maxFreq improves, so a stale value can't produce a wrong longer result. The common pitfall is recomputing the max over 26 letters every step, which is still fine, or trying a DP table over substrings, which blows past 100000. Another pitfall is forgetting k = 0, where the answer is just the longest run. If you freeze in the live OA, StealthCoder can hand you the window loop as a hedge. Answer is the largest window size seen.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Longest Repeating Character Replacement 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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as longest repeating character replacement. 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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Longest Repeating Character Replacement FAQ
What's the trick in Longest Repeating Character Replacement?+
Use a sliding window with a count array. A window is valid if its length minus the highest letter count is at most k. That difference equals the replacements needed. Expand right every step, shrink left only when invalid, and record the max window length.
Why isn't this really a dynamic programming problem?+
The hint says DP, but nothing here needs a table of overlapping subproblems. The window only moves forward, and each character is added and removed at most once. That gives O(n) time and O(26) space, which is what the 100000 length demands.
Do I need to update maxFreq when the window shrinks?+
No. Leave it stale. The answer can only grow if a letter's count in the window exceeds the old maxFreq, and that updates it naturally. A stale higher value never lets an invalid longer window sneak into the result, so it's safe and faster.
What edge cases should I test before submitting?+
Test k = 0, where you need the longest run of one letter. Test k equal to the string length, where the answer is the full length. Test a single character string. Also check all-distinct letters like ABCDE with k = 1, which should return 2.
How do I prepare for this in 48 hours?+
Write the sliding window solution from scratch three times until the shrink condition is automatic. Then do one similar problem, like max consecutive ones with k flips. Know the complexity: O(n) time, constant space. Microsoft-style OAs reward clean, bug-free window code over fancy ideas.