Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
Given a binary array nums, return the maximum length of a contiguous subarray with an equal number of 0 and 1.
Example 1:
Input: nums = [0,1] Output: 2 Explanation: [0, 1] is the longest contiguous subarray with an equal number of 0 and 1.
Example 2:
Input: nums = [0,1,0] Output: 2 Explanation: [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1.
Example 3:
Input: nums = [0,1,1,1,1,1,0,0,0] Output: 6 Explanation: [1,1,1,0,0,0] is the longest contiguous subarray with equal number of 0 and 1.
Constraints:
1 <= nums.length <= 105nums[i] is either 0 or 1.Problem summary: Given a binary array nums, return the maximum length of a contiguous subarray with an equal number of 0 and 1.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[0,1]
[0,1,0]
[0,1,1,1,1,1,0,0,0]
maximum-size-subarray-sum-equals-k)find-all-possible-stable-binary-arrays-i)find-all-possible-stable-binary-arrays-ii)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #525: Contiguous Array
class Solution {
public int findMaxLength(int[] nums) {
Map<Integer, Integer> d = new HashMap<>();
d.put(0, -1);
int ans = 0, s = 0;
for (int i = 0; i < nums.length; ++i) {
s += nums[i] == 1 ? 1 : -1;
if (d.containsKey(s)) {
ans = Math.max(ans, i - d.get(s));
} else {
d.put(s, i);
}
}
return ans;
}
}
// Accepted solution for LeetCode #525: Contiguous Array
func findMaxLength(nums []int) int {
d := map[int]int{0: -1}
ans, s := 0, 0
for i, x := range nums {
if x == 0 {
x = -1
}
s += x
if j, ok := d[s]; ok {
ans = max(ans, i-j)
} else {
d[s] = i
}
}
return ans
}
# Accepted solution for LeetCode #525: Contiguous Array
class Solution:
def findMaxLength(self, nums: List[int]) -> int:
d = {0: -1}
ans = s = 0
for i, x in enumerate(nums):
s += 1 if x else -1
if s in d:
ans = max(ans, i - d[s])
else:
d[s] = i
return ans
// Accepted solution for LeetCode #525: Contiguous Array
struct Solution;
use std::collections::HashMap;
impl Solution {
fn find_max_length(nums: Vec<i32>) -> i32 {
let mut res: usize = 0;
let mut hm: HashMap<i32, usize> = HashMap::new();
let mut diff = 0;
let n = nums.len();
hm.entry(0).or_default();
for i in 0..n {
if nums[i] == 1 {
diff += 1;
} else {
diff -= 1;
}
let j = *hm.entry(diff).or_insert(i + 1);
res = usize::max(i + 1 - j, res);
}
res as i32
}
}
#[test]
fn test() {
let nums = vec![0, 1];
let res = 2;
assert_eq!(Solution::find_max_length(nums), res);
let nums = vec![0, 1, 0];
let res = 2;
assert_eq!(Solution::find_max_length(nums), res);
}
// Accepted solution for LeetCode #525: Contiguous Array
function findMaxLength(nums: number[]): number {
const d: Record<number, number> = { 0: -1 };
let ans = 0;
let s = 0;
for (let i = 0; i < nums.length; ++i) {
s += nums[i] ? 1 : -1;
if (d.hasOwnProperty(s)) {
ans = Math.max(ans, i - d[s]);
} else {
d[s] = i;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.