Reported December 2025
HackerRankprefix sum

Longest Equal Binary Subarray After One Flip

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

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

The mistake that sinks a first attempt on this HackerRank problem is treating it like the classic longest balanced subarray and forgetting the flip changes the target. It was reported in December 2025, and the input is a binary array up to 100000 long. You can flip one element, then you want the longest subarray with equal 0s and 1s. Think prefix sums, not brute force. If you're taking it in the next day or two, learn the sum trick below. StealthCoder is the quiet safety net on the live OA if your mind goes blank mid-problem.

The problem

You are given a binary array arr containing only 0s and 1s.
You may flip at most one element of the array, changing a 0 to a 1 or a 1 to a 0.
Return the length of the longest contiguous subarray that can contain an equal number of 0s and 1s after performing at most one flip.

Function
longestEqualBinarySubarrayAfterOneFlip(arr: int[]) → int

Examples
Example 1
arr = [0,1,0,0,1,1,0,0]
return = 8
The whole array has five 0s and three 1s. Flip one 0 to 1 to make the counts equal.
Example 2
arr = [0,0,0,1]
return = 4
Flip one 0 in the whole array, leaving two 0s and two 1s.
Example 3
arr = [1,1,1]
return = 2
Any length-2 subarray can be balanced by flipping one 1 to 0.

Constraints
1 <= arr.length <= 100000
arr[i] is either 0 or 1

Reported by candidates. Source: FastPrep

Pattern and pitfall

Map 0 to -1 and 1 to +1, then track prefix sums. A subarray is balanced when its sum is 0. One flip changes the sum by +2 or -2, so a subarray works if its sum is exactly 0, 2, or -2. Even length is required, and the sum of 2 or -2 means the flip fixes it. Store the first index of each prefix value in a hash map. For each position, look up prefix, prefix-2, and prefix+2 and take the max length. Pitfall one: only checking sum 0 and missing flip cases. Pitfall two: odd lengths can never balance, but sum +-2 already guarantees even length, so parity takes care of itself. Example 3, [1,1,1], gives 2 because sum 2 over length 2 flips one 1. Runs in O(n) time. StealthCoder is your hedge on the live OA if the prefix-offset idea slips.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Longest Equal Binary Subarray After One Flip 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 for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ The honest play

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

HackerRank reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Longest Equal Binary Subarray After One Flip FAQ

What's the trick for this HackerRank problem?+

Convert 0 to -1 and 1 to +1, then use prefix sums with a hash map of first occurrences. A flip shifts a subarray sum by 2, so valid subarrays have sum 0, 2, or -2. Look up prefix, prefix-2, and prefix+2 at each index and keep the longest span.

Why isn't the plain balanced subarray solution enough?+

The plain version only accepts sum 0. Here one flip lets you fix a subarray whose sum is +2 or -2, like [1,1,1] returning 2 or [0,0,0,1] returning 4. Ignoring the flip gives wrong answers on exactly those cases.

What time complexity should I aim for?+

O(n) time and O(n) space. With an array up to 100000 elements, a nested loop checking every subarray is O(n^2) and will likely time out. One pass with a hash map of first-seen prefix sums is the target.

How do I handle edge cases like all 1s or length 1?+

All 1s of length 3 returns 2, since you flip one to balance a pair. A length-1 array can't be balanced, because a single flip still leaves one element, so the answer there is 0. Seed your map with prefix 0 at index -1 to handle subarrays starting at the front.

How do I prepare for this in 48 hours?+

Write the balanced subarray prefix-sum solution from memory, then extend it with the +2 and -2 lookups. Test on the three examples by hand. Spend the rest of the time on hash map prefix-sum variants so the pattern feels automatic.

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

OA at HackerRank?
Invisible during screen share
Get it