Reported February 2026
IBMstack

Longest Balanced Binary Subarray

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

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

The IBM OA reported in February 2026 hands you a binary array and asks for the longest subarray that is a valid parenthesis string in disguise. Treat 1 as an open paren and 0 as a close paren. Equal counts plus every prefix having at least as many 1s as 0s is exactly a balanced bracket sequence. The hinted pattern is dynamic programming, but you don't need a full table. If you've got an invite in your inbox, learn the reduction and you're mostly done. StealthCoder sits invisibly as a safety net if your mind goes blank mid-assessment.

The problem

You are given an integer array arr containing only 0s and 1s.
Return the length of the longest contiguous subarray that satisfies both conditions:
The subarray contains the same number of 0s and 1s.
For every prefix of the subarray, the number of 1s is greater than or equal to the number of 0s.

Function
longestBalancedBinarySubarray(arr: int[]) → int

Examples
Example 1
arr = [1, 0, 1, 1, 0, 0, 1]
return = 6
The subarray [1,0,1,1,0,0] is balanced and every prefix has at least as many 1s as 0s.
Example 2
arr = [0, 1, 1, 0]
return = 2
The longest valid subarray is [1,0]. The full array is balanced, but its first prefix starts with more 0s than 1s.
Example 3
arr = [1,0,1,1,0,0,1]
return = 6
The subarray [1,0,1,1,0,0] is balanced, and every prefix has at least as many 1s as 0s.
Example 4
arr = [0,1,1,0]
return = 2
The full array is balanced, but its first prefix has more 0s than 1s. The longest valid subarray is [1,0].

Constraints
1 <= arr.length <= 2 * 10^5
arr[i] is either 0 or 1.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Map 1 to +1 and 0 to -1. A valid subarray never dips below its starting balance and ends back at it. That's the longest valid parentheses problem. The clean O(n) approach uses a stack of indices. Push -1 initially. For each 1, push its index. For each 0, pop. If the stack empties, push the current index as the new base. Otherwise the answer candidate is i minus the stack top. The DP alternative is dp[i] = length of the valid run ending at i, which also works. The common pitfall is using the plain prefix-sum hash map for equal 0s and 1s. That finds balanced subarrays but ignores the prefix condition, so it returns 4 on [0,1,1,0] instead of 2. Watch for that. If you freeze during the live OA, StealthCoder can surface the stack solution while you keep your pace.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Longest Balanced 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. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as longest valid parentheses. If you have time before the OA, drill that.

⏵ The honest play

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

IBM reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Longest Balanced Binary Subarray FAQ

What's the trick in Longest Balanced Binary Subarray?+

It's longest valid parentheses. Treat 1 as an open bracket and 0 as a close bracket. The prefix condition rules out the usual equal-count hash map trick, so you need a stack of indices or a DP over ending positions.

Why does the prefix-sum hash map approach fail here?+

It only checks that the counts of 0s and 1s are equal. On [0,1,1,0] it returns 4, but the expected answer is 2 because the first prefix has more 0s than 1s. You need the never-dip-below-zero rule too.

What complexity should I aim for with 2 * 10^5 elements?+

O(n) time. The array can hold 200,000 elements, so anything quadratic is risky. The stack solution makes one pass with O(n) extra space. A DP version is also O(n) and uses an array of lengths.

Is this IBM question still being asked?+

It was reported in February 2026, so it's recent. The same example appears twice in the report, which suggests it's a live problem. Expect variations in the input but the same bracket-matching core.

How do I prepare in 48 hours?+

Solve longest valid parentheses both ways, stack and DP. Then rewrite it with 1 and 0 as the input. Test it on [0,1,1,0], [1,0,1,1,0,0,1], and an all-zeros array so you catch the reset-the-base case.

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

OA at IBM?
Invisible during screen share
Get it