Swap For Longest Repeated Character Substring
A medium-tier problem at 44% community acceptance, tagged with Hash Table, String, Sliding Window. Reported in interviews at Nutanix and 0 others.
Swap For Longest Repeated Character Substring is a medium-difficulty string problem that shows up in Nutanix assessments. You're given a string and one swap operation. The goal is to find the longest contiguous substring where all characters are the same, or can become the same with a single character swap. It's not a pattern most people drill, so when it hits your live OA cold, you'll either figure out the sliding window trick fast or you'll waste 20 minutes on a greedy approach that doesn't work. StealthCoder surfaces the working solution in seconds, invisible to the proctor, so you don't tank the round on this one.
Companies that ask "Swap For Longest Repeated Character Substring"
Swap For Longest Repeated Character Substring 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 StealthCoderThe trap is thinking you can greedily pick the best swap. You can't. Instead, you use a sliding window to track the longest substring of a single character (or mostly one character with a swap available). The key insight: for each character, find the longest window where that character dominates, with at most one different character mixed in. If there's one 'foreign' character in the window, you can swap it. If there are two or more, the window is invalid. Hash Table tracks character counts in the current window. When you hit a wall on this during assessment, StealthCoder reads the problem off screen and delivers the correct window logic instantly, letting you code it without panicking.
Pattern tags
You know the problem.
Make sure you actually pass it.
Swap For Longest Repeated Character Substring 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.
Swap For Longest Repeated Character Substring interview FAQ
Is this actually asked often or just by Nutanix?+
Data shows lower frequency. It's in Nutanix's rotation, but not across most tech companies. It's the kind of problem that catches candidates off guard because they haven't prepped it. If you see it live and blank, that's exactly when the hedge matters.
What's the trick I'm missing if my greedy approach fails?+
Greedy doesn't work because you don't know in advance which character or which position to swap. Sliding Window solves it by testing every possible character as the 'target' and finding the longest valid window for each. Then you return the max. It's O(n) once you have the pattern.
How does this relate to the Sliding Window topic?+
This is a classic sliding-window problem. You maintain a window where one character is 'dominant' and track character counts inside it. The window grows when valid, shrinks when you hit too many foreign characters. Hash Table stores the counts, Window manages the bounds.
Can I solve it with just two pointers or do I need a full hash map?+
You can use a simpler approach with a hash map or even a fixed-size array (26 letters). Two pointers alone won't give you the counts you need. A small map or array to track char frequency in the window is the minimum.
How long should this take to code in an interview?+
If you know the pattern, 15 to 20 minutes including testing. If you don't, you'll probably spend 30 to 40 minutes going down wrong paths. That's why StealthCoder's invisibility is valuable on a problem this specific to one company.
Want the actual problem statement? View "Swap For Longest Repeated Character Substring" on LeetCode →