Longest Repeating Character Replacement
Reported by candidates from ByteDance's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
With s.length up to 100000, trying every substring and every target letter is dead on arrival, and that's the whole point of this ByteDance question reported in July 2025. It's Longest Repeating Character Replacement. You get a string of uppercase letters and a budget of k replacements, and you need the longest window that can become one repeated letter. The hint says dynamic programming, but the real answer is a sliding window with a letter count array. If you blank halfway through, StealthCoder sits invisibly on your screen during the live OA and hands you the window logic so you can keep typing.
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
The trick: a window is valid if its length minus the count of its most frequent letter is at most k. That difference is how many characters you'd need to replace. Keep a 26-slot count array, move the right pointer, update the count, and track maxFreq, the highest count seen in any window so far. If window size minus maxFreq exceeds k, move the left pointer one step and decrement its count. The window never shrinks, it just slides, so the answer is the final window size. The common pitfall is recomputing the true max on every shrink, or thinking maxFreq must decrease when you shrink. It doesn't, because only a larger maxFreq can grow the answer. Runtime is O(n) with O(26) space. Don't reach for DP, it's the wrong tool. If the invariant slips away under pressure, StealthCoder is your safety net during the live OA.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
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 by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
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 ByteDance's OA.
ByteDance reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Longest Repeating Character Replacement FAQ
What's the trick in Longest Repeating Character Replacement?+
Check validity with window length minus the highest letter count in the window. If that's at most k, the window can be fixed with k replacements. Slide the right pointer, and move the left pointer only when the window turns invalid. One pass, O(n).
Is this really a dynamic programming problem?+
No. The hint says DP, but the clean solution is a sliding window with two pointers and a 26-letter count array. DP would be slower and more complicated. Treat the label as noise and go with the window.
Why doesn't maxFreq need to be decreased when the window shrinks?+
The answer only improves when a window gets a higher max frequency than before. A stale, larger maxFreq can't produce a false longer answer, because the window size doesn't grow unless a real higher count appears. So skipping the recompute is safe and keeps it O(n).
How big a deal is the 100000 length constraint?+
It rules out O(n^2) brute force over all substrings, and O(26 * n^2) is worse. You need linear or near-linear time. The sliding window gives O(n) time with a fixed 26-slot array, which fits comfortably.
How do I prep for this ByteDance OA in 48 hours?+
Code the sliding window from memory twice. Test with ABAB and k=2, then AABABBA and k=1. Check edge cases: k=0, a single character, and k equal to the length. Be able to explain why the window never shrinks. That covers most variants of this problem.