Uber · Question 3 · Arrays
Find the largest subarray with equal number of 0s and 1s
Problem
Find max length subarray with equal 0s and 1s.
Approach
Replace 0 with - 1, use hashmap of prefix sums.
Time O(n)Space O(n)
How it works
- 1Treat 0 as -1 so equal counts mean prefix sum zero.
- 2Record the first index of every prefix sum.
- 3A repeated sum marks a balanced subarray; keep the longest.
Sample input / output
Input: 3 0 1 0 Output: 2
Constraints
1 <= nums.length <= 10^5; nums[i] is 0 or 1.
Solution — read it, then walk it
Time O(n)Space O(n)Loops 1Branches 3Returns 1source · Python
Scroll sideways for wide charts — the view follows the active step.
- start / end
- statement
- condition
- loop
- returns
- definition
1/21
StartSTART
Interactive control-flow diagram and step-by-step code walkthrough.
Recommended resourcesAffiliate links · may earn us a commission
Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits