MEDIUMasked at 16 companies

Longest Repeating Character Replacement

A medium-tier problem at 57% community acceptance, tagged with Hash Table, String, Sliding Window. Reported in interviews at Pocket Gems and 15 others.

Founder's read

Longest Repeating Character Replacement hits your assessment screen and looks deceptively simple: given a string and a replacement budget, find the longest substring where you can make all characters the same by changing at most k characters. Amazon, ByteDance, and Flipkart ask it frequently. The 57% acceptance rate signals the trap: the greedy approach fails silently, and the sliding window logic isn't obvious on first read. This is the kind of problem where five minutes of confusion costs you. If it appears in your live OA and you freeze on the window shrink logic, StealthCoder surfaces a working solution invisible to the proctor.

Companies asking
16
Difficulty
MEDIUM
Acceptance
57%

Companies that ask "Longest Repeating Character Replacement"

If this hits your live OA

Longest Repeating Character Replacement is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by a senior engineer who knows the OA is theater. This is the script.

Get StealthCoder
What this means

The trick is sliding window with a frequency map. Expand the window, track character counts, and shrink only when the window violates the constraint: max frequency plus k must still be less than window size. Most candidates try to track every character's count manually and fail on boundary cases. Others skip the window shrink entirely and blow their time limit. The pattern combines Hash Table to count occurrences and Sliding Window to track the valid range, but the relationship between max frequency and k is where candidates blank. The String itself is just the input container. Once you lock the constraint check, the code flows. On a live OA, if the pattern doesn't click in the first two minutes, StealthCoder removes the risk by handing you the exact condition and the loop structure, so you're not guessing at whether to shrink left or right.

Pattern tags

The honest play

You know the problem. Make sure you actually pass it.

Longest Repeating Character Replacement recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by a senior engineer who knows the OA is theater. This is the script. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Longest Repeating Character Replacement interview FAQ

Is this really asked at big tech companies?+

Yes. Amazon, ByteDance, and Flipkart have all reported it. With 16 companies total in the dataset and a 57% acceptance rate, it's a mid-tier favorite: hard enough to filter weak candidates, simple enough to implement in one sitting. Expect it in phone screens and OA rounds.

What's the main gotcha?+

The window shrink condition. You don't shrink when frequency is invalid. You shrink only when (max frequency + k) < window length. Most candidates try tracking every character or forget to shrink at all. The max frequency check is the entire trick.

How does Sliding Window connect to Hash Table here?+

Hash Table stores character frequencies so you can check the max in O(1) per operation. Sliding Window controls which characters are in scope. Neither works alone. You expand, update the hash table, check the constraint, then shrink if needed. It's the standard window plus state pattern.

What if k is 0?+

Then you're finding the longest substring of a single repeated character. The constraint becomes max frequency must equal window length. Test this edge case early if you get this problem live. It clarifies the logic.

How much time should this take in a real OA?+

If the pattern clicks, 12 to 18 minutes. If it doesn't, you can lose 30+ minutes chasing wrong approaches. The window shrink logic isn't taught in most courses, so if you haven't seen it, StealthCoder is the hedge that saves 20 minutes and a failed submission.

Want the actual problem statement? View "Longest Repeating Character Replacement" on LeetCode →

Frequency and company-tag data sourced from public community-maintained interview-report repos. Problem, description, and trademark © LeetCode. StealthCoder is not affiliated with LeetCode.