Reported January 2026
Motiveprefix sum

Longest Equal-Zero-One Subarray

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

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

The function is called longestBalancedSubarray, and the whole job is finding the longest stretch with the same number of 0s and 1s. Motive reported this OA in January 2026, and the input can run to 100000 elements, so a brute-force pair check won't survive. The pattern is a prefix sum with a hash map, not real dynamic programming, even though it gets tagged that way. If you've seen it once, it takes ten minutes. If you blank, StealthCoder is the invisible safety net running during the live OA.

The problem

Given a binary array nums, return the maximum length of a contiguous subarray containing the same number of 0s and 1s.

Function
longestBalancedSubarray(nums: int[]) → int

Examples
Example 1
nums = [0,1]
return = 2
The full array contains one zero and one one.
Example 2
nums = [0,1,0]
return = 2
Either length-two prefix is balanced.

Constraints
0 <= nums.length <= 100000.
Every value is 0 or 1.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Treat every 0 as -1 and every 1 as +1. Now a balanced subarray is just a subarray that sums to zero. Keep a running sum and a hash map from sum to the first index where it appeared. Seed the map with sum 0 at index -1. When you see a sum again at index i, the length is i minus the first index, so update the max. Never overwrite the first index, because you want the longest span. The common pitfall is forgetting the seed, which breaks cases like [0,1] where the answer starts at index 0. Another is storing the last index instead of the first. Empty input returns 0. It's O(n) time and O(n) space. If the logic slips under pressure, StealthCoder can hand you the clean version during the live OA.

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-Zero-One 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 Motive's OA.

Motive 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-Zero-One Subarray FAQ

What's the trick to Longest Equal-Zero-One Subarray?+

Map 0 to -1 and 1 to +1, then look for the longest subarray with sum zero. Use a running sum and a hash map storing the first index of each sum. Same sum seen twice means the stretch between is balanced.

Is this really dynamic programming?+

Not in practice. It gets tagged that way, but the solution is a prefix sum plus a hash map. There's no recurrence or table to fill. Don't waste time hunting for a DP state. One pass and a dictionary does it.

Why seed the map with sum 0 at index -1?+

It handles balanced subarrays that start at index 0. For [0,1], the running sum hits 0 at index 1. Without the seed there's nothing to match, and you'd return 0 instead of 2. This is the most common bug.

What edge cases should I test?+

Test an empty array, which returns 0. Test a single element, which returns 0. Test all zeros or all ones, also 0. Test [0,1,0], which returns 2. With up to 100000 elements, make sure you're O(n), not O(n^2).

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

Write the prefix-sum-with-hash-map solution from scratch twice. Then do two related problems like subarray sum equals k. Motive reported this in January 2026, so the pattern is current. Focus on recognizing the sum-to-zero transform fast.

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

OA at Motive?
Invisible during screen share
Get it