Reported December 2025
HackerRankprefix sum

Longest Equal Binary Subarray

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 HackerRank OA reported in December 2025 has a problem that looks like dynamic programming and isn't. Longest Equal Binary Subarray gives you a 0/1 array and asks for the longest stretch with matching counts. Most people reach for nested loops, which dies at 100000 elements. The real answer is a prefix sum with a hash map, and one edge case trips up the naive version. If you blank during the live assessment, StealthCoder runs invisibly on your desktop and gives you the approach in real time. But you can learn this one in ten minutes.

The problem

You are given a binary array arr containing only 0s and 1s.
Return the length of the longest contiguous subarray that contains an equal number of 0s and 1s.

Function
longestEqualBinarySubarray(arr: int[]) → int

Examples
Example 1
arr = [0,1,0,0,1,1,0]
return = 6
The subarray [1,0,0,1,1,0] has three 0s and three 1s.
Example 2
arr = [0,0,1,1]
return = 4
The entire array is balanced.
Example 3
arr = [1,1,1]
return = 0
No non-empty subarray has an equal number of 0s and 1s.

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

Reported by candidates. Source: FastPrep

Pattern and pitfall

Treat every 0 as -1 and every 1 as +1. Now you want the longest subarray summing to zero. Keep a running sum and a hash map from sum to the first index where it appeared. When you see the same sum again at index i, the stretch between the first occurrence and i is balanced. The edge case is the start of the array. Seed the map with {0: -1}, otherwise a balanced prefix like [0,1,0,0,1,1] gets missed, and Example 2 returns 0 instead of 4. Only store the first index, never overwrite it, or you shrink your answer. Return 0 if nothing matches, like [1,1,1]. That's O(n) time and O(n) space. If you freeze on the live OA, StealthCoder is the hedge, but the seed entry is the whole trick.

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 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

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as contiguous array. If you have time before the OA, drill that.

⏵ 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 FAQ

What's the trick for Longest Equal Binary Subarray?+

Map 0 to -1 and 1 to +1, then find the longest subarray with sum zero. Track a running sum in a hash map keyed by sum, storing the first index seen. A repeated sum means the segment between those indices is balanced. It runs in linear time.

Why does my solution fail on arrays like [0,0,1,1]?+

You probably forgot to seed the map with sum 0 at index -1. Without it, a balanced prefix has nothing to match against, so the answer comes out too small. Example 2 should return 4, and the seed is what makes that work.

Is this really dynamic programming?+

Not in the classic sense. The hinted label is dynamic programming, but the working solution is a prefix sum plus hash map. You carry a running state forward, which feels DP-ish, but there's no table or recurrence to fill in.

Will brute force pass the constraints?+

No. With arr.length up to 100000, checking every subarray is O(n^2) or worse, which means billions of operations. You'll time out on large tests. The single-pass hash map approach is what the constraints are pushing you toward.

How do I prepare for this in 48 hours?+

Write the solution from memory three times, using the -1/+1 transform and the seeded map. Then test it on [1,1,1], [0,0,1,1], and the 7-element example. Once that feels automatic, try related prefix-sum problems so the pattern sticks.

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