Bitwise ORs of Subarrays
MediumBit Manipulationbit manipulation
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 an integer array arr, consider every non-empty contiguous subarray and compute the bitwise OR of its elements (a one-element subarray's OR is the element itself).
Return how many **distinct** values these ORs take. Aim for roughly O(n log(max)) time rather than enumerating all O(n^2) subarrays.
Examples
Input: arr = [0]
Output: 1
The only subarray has OR 0.
Input: arr = [1,1,2]
Output: 3
The subarray ORs are 1, 1, 2, 1, 3, 3, giving the distinct values {1, 2, 3}.
Input: arr = [1,2,4]
Output: 6
The distinct ORs are 1, 2, 4, 3, 6 and 7.
Constraints
- `1 <= arr.length <= 5 * 10^4`
- `0 <= arr[i] <= 10^9`
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.