Longest Repeating Character Replacement
Reported by candidates from DigitalOcean's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The usual way to sink your first attempt at this DigitalOcean OA question, reported in September 2026, is to treat it like a DP problem and burn twenty minutes on a table you don't need. It's Longest Repeating Character Replacement. You get a string of uppercase letters and a budget of k replacements, and you want the longest window that can become one repeated letter. It's a sliding window with a frequency count, and the code is maybe ten lines. If you blank on the window logic mid-assessment, StealthCoder runs invisibly as a safety net and shows you the working approach.
The problem
Given a string s and an integer k, you may replace at most k characters with other uppercase English letters. Return the maximum length of a contiguous substring that can contain only one repeated character after these replacements. For this exercise, assume the input contains only uppercase English letters. Return 0 for an empty string; replacements may be unused. Function characterReplacement(s: String, k: int) → int Examples Example 1 s = "ABAB" k = 2 return = 4 Replace both B characters to obtain AAAA. Example 2 s = "AABABBA" k = 1 return = 4 The first four characters AABA can become AAAA with one replacement. Every length-five window needs at least two. Example 3 s = "" k = 0 return = 0 An empty string has length zero. Constraints 0 <= s.length <= 10^5. 0 <= k <= s.length. Every character is between A and Z.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: a window is valid when its length minus the count of its most frequent letter is at most k. That difference is how many characters you'd have to replace. Expand the right pointer, update the count array of 26 letters, track maxFreq, and if the window needs more than k replacements, move the left pointer once. The pitfall is recomputing the true max frequency after every shrink. You don't have to. maxFreq only matters when it grows, because only then can the window grow past its previous best. Stale maxFreq never produces a wrong answer, since the answer is the largest window ever seen. Also handle the empty string and k equal to zero cleanly. The loop handles both if you return the best length. It's O(n) time and O(1) space. If the pointer logic slips under pressure, StealthCoder is the hedge during the live OA.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
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. 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
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 DigitalOcean's OA.
DigitalOcean 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.
Longest Repeating Character Replacement FAQ
What's the trick to Longest Repeating Character Replacement?+
Use a sliding window. A window is valid if its length minus its most frequent letter count is at most k. Expand right every step, shrink left only when invalid. Track the best length seen. That's the whole solution, linear time.
Is this really a dynamic programming problem?+
No. DP is a tempting wrong turn. Sliding window with a 26-slot count array solves it in O(n) time and O(1) space. A DP table over substrings would be O(n^2) and too slow for a string up to 10^5 characters.
Why don't I need to recompute maxFreq when the window shrinks?+
Only a larger maxFreq can produce a larger valid window. If maxFreq is stale and too high, the window just won't grow, so it never beats the recorded best. The answer stays correct, and you skip a 26-step scan per move.
What edge cases should I test before submitting?+
Test the empty string, which returns 0. Test k equal to 0, where you want the longest run of one letter. Test k equal to the string length, where the answer is the full length. Also try a single character and an all-same-letter string.
How do I prepare for this in 48 hours?+
Write the sliding window from scratch twice without looking. Then trace Example 2, AABABBA with k=1, by hand until you see why the answer is 4. Practice explaining the validity condition out loud. It's a pattern you can lock in within one evening.