Kth Largest Subarray Bitwise OR
Reported by candidates from Uber's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Uber reported this one in February 2026, and the first thing you notice is that k is a long. The number of subarrays is n(n+1)/2, so listing every subarray OR and sorting them is dead on arrival. Brute force is what the problem wants you to try first. The real question is how to count or locate the k-th largest OR without ever enumerating all of them. If you're taking this OA in the next day or two, learn the one property that makes it tractable: OR only grows as a subarray extends. If you blank mid-assessment, StealthCoder runs invisibly on your desktop as a safety net and can walk you to the solution.
The problem
You are given an integer array nums and an integer k. Consider every non-empty contiguous subarray of nums, and compute the bitwise OR of the elements in that subarray. Return the k-th largest value among all subarray bitwise OR values. If the same OR value is produced by multiple subarrays, each occurrence is counted separately. Function kthLargestSubarrayOr(nums: int[], k: long) → int Examples Example 1 nums = [1, 2, 3] k = 5 return = 2 The subarray OR values are 1, 3, 3, 2, 3, 3. In descending order they are 3, 3, 3, 3, 2, 1, so the 5th largest value is 2. Example 2 nums = [5, 1] k = 2 return = 5 The OR values are 5, 5, and 1. The 2nd largest occurrence is still 5. Constraints 1 <= nums.length <= 105 0 <= nums[i] <= 109 1 <= k <= nums.length * (nums.length + 1) / 2
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that OR is monotone. Extending a subarray to the right can only keep the value the same or set more bits. So for a fixed right end, the OR values of subarrays ending there form a chain with at most about 30 distinct values, since each change adds at least one bit. Keep a list of (value, count) pairs per right end, merge equal values, and add the counts into a global map. Then sort the distinct values descending and accumulate counts until you reach k. That's roughly 30n work. The binary search route also works: guess x, count subarrays with OR >= x using two pointers and bit counters. The pitfall is counts. Use 64-bit integers, and count each subarray occurrence separately, as Example 2 shows. If the live OA rattles you, StealthCoder is the hedge for exactly this kind of blank.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Kth Largest Subarray Bitwise OR 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 by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Uber's OA.
Uber reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Kth Largest Subarray Bitwise OR FAQ
What's the trick for Kth Largest Subarray Bitwise OR?+
OR never decreases when you extend a subarray. That means subarrays ending at a given index produce only about 30 distinct OR values. Track value and count pairs per right end, merge duplicates, then walk values in descending order until the cumulative count reaches k.
Why does brute force fail here?+
There are n(n+1)/2 subarrays, and k is passed as a long because that count gets huge. Computing every OR and sorting them blows both time and memory. You need to group subarrays by OR value and use counts instead of listing them.
Do duplicate OR values count separately?+
Yes. Example 2 with [5, 1] and k = 2 returns 5 because the OR values are 5, 5, and 1. Each subarray that produces a value counts as its own occurrence, so you must store counts per value, not just a set of distinct values.
Should I use binary search or the distinct-values approach?+
Either works. Binary search on the answer needs a counting function for OR >= x, which uses two pointers with per-bit counters. The distinct-values approach is simpler to code and has fewer edge cases, so pick it unless you already know the binary search pattern cold.
How do I prepare for this in 48 hours?+
Write the per-right-end list of (OR, count) pairs once from scratch. Then test it on both examples, a single-element array, and an all-zeros array. Check that your counts use 64-bit integers. If that passes, you've covered the real pitfalls of this problem.