Reported July 2026
Tekionprefix sum

Longest Balanced Substring After One Swap

Reported by candidates from Tekion's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Tekion OA. Under 2s to a working solution.
Founder's read

With s.length up to 10^5, trying every swap and scanning every substring is dead on arrival. That's the wall in the Tekion question reported in July 2026, Longest Balanced Substring After One Swap. It looks like a swap puzzle, but it's a prefix-sum problem with one small twist. If you've seen the classic equal zeros and ones substring, you're most of the way there. If your head goes blank when the clock starts, StealthCoder sits invisibly on your screen during the live OA and gives you the approach and code. Read the trick below first, though. It's short, and knowing it makes the whole assessment feel less like theater.

The problem

You are given a binary string s consisting only of '0' and '1'.
A string is balanced when it contains an equal number of '0' and '1' characters.
You may swap any two characters in s at most once. After the optional swap, select a balanced substring of s.
Return the maximum possible length of the selected balanced substring.

Function
longestBalancedSubstringAfterOneSwap(s: String) → int

Examples
Example 1
s = "100001"
return = 4
Swap the third character with the final character to obtain 101000. Its prefix 1010 is balanced, with two zeroes and two ones.
Example 2
s = "111"
return = 0
No non-empty balanced substring can be formed, so the maximum length is 0.

Constraints
1 <= s.length <= 10^5.
s contains only '0' and '1'.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Map 1 to +1 and 0 to -1, then take prefix sums. A balanced substring is a pair of equal prefix values, so a hash map of first occurrences gives the no-swap answer in O(n). Now the swap. Swapping two characters inside the substring changes nothing. Swapping one inside with a different one outside shifts the difference by exactly 2. So after the swap, a substring works if its difference is 0, or if it's off by 2 and the missing character exists outside it. Check prefix pairs that differ by 0, +2 and -2, then verify the outside count. The pitfall is that the longest pair isn't always valid when there aren't enough outside characters, so test the count before accepting it. Example 2, "111", returns 0 because no zero exists anywhere. If you freeze during the live OA, StealthCoder is the hedge that hands you a working solution quietly.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Longest Balanced Substring After One Swap 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 by an Amazon engineer who passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Tekion's OA.

Tekion reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Longest Balanced Substring After One Swap FAQ

What's the trick in Longest Balanced Substring After One Swap?+

Turn the string into +1 and -1 and use prefix sums. One swap can only fix a substring whose difference is exactly 2, because an inside-outside swap moves the difference by 2. So you look for balanced substrings plus substrings off by 2 that have a spare character of the right type outside them.

How hard is this Tekion OA question really?+

Medium. The no-swap version is a well-known hash map and prefix sum problem. The swap adds a reasoning step about difference 2 and outside character availability. Once you see that, the code is short. The hard part is the edge cases, not the algorithm.

What time complexity do I need with n up to 10^5?+

Aim for O(n). Brute force over all swaps and substrings is O(n^3) or worse and will time out. A single pass with prefix sums and a hash map of first-seen positions fits easily. Counting total ones and zeros up front is O(n) too.

What edge cases should I test before submitting?+

Test all ones like "111", which returns 0, and all zeros. Test a string that's already balanced, a single character, and a case like "100001" where the answer is 4 and the whole string fails because no spare one sits outside. Also test lengths near the cap of twice the smaller count.

How do I prepare in 48 hours for an OA like this?+

Nail the equal zeros and ones substring pattern first, since it's the base case. Then write out why a swap shifts the difference by 2 and code the outside-count check. Run your solution on the two examples and a few tiny strings by hand. One focused hour on prefix sums beats skimming ten problems.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Tekion.

OA at Tekion?
Invisible during screen share
Get it