Contiguous Array
MediumArrays & Hashingprefix sum
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
Given a binary array nums, return the length of the longest contiguous subarray that contains an equal number of 0s and 1s. Return 0 if no such subarray exists.
Aim for O(n) time.
Examples
Input: nums = [0,1]
Output: 2
The whole array has one 0 and one 1.
Input: nums = [0,1,0]
Output: 2
Either [0, 1] or [1, 0] works.
Input: nums = [0,0,1,0,0,0,1,1]
Output: 6
nums[2..7] = [1, 0, 0, 0, 1, 1] has three of each.
Constraints
- 1 <= nums.length <= 10^5
- `nums[i]` is either 0 or 1
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.