The Shortest Subarray With OR at Least K I interview question asks you to find the length of the shortest subarray of a non-negative integer array whose bitwise OR is at least K. If no such subarray exists, return -1. This is the easier version (EASY) where the array values are small enough for a brute-force approach to work within time limits.
Mitsogo asks this problem as an entry-level bitwise operation exercise. It tests knowledge of the bitwise OR operation's monotonic property: OR can only increase (or stay the same) as more elements are added to a window. This property enables sliding window or brute-force approaches. Understanding bitwise operations at the subarray level is foundational for harder bit-manipulation problems.
The pattern is brute-force nested loop or sliding window. Nested loop: for each start index i, expand the subarray to the right computing running OR. Once the OR reaches or exceeds K, update the minimum length and break (adding more elements can't decrease the OR). This is O(n^2) but acceptable for small inputs. Alternatively, use a sliding window: since OR only grows, once OR ≥ K, try shrinking the left side. However, OR doesn't support efficient "undo" — track bit counts per position to enable shrinking.
Array: [2, 1, 8, 3], K = 10.
Minimum length: 2 (subarray [8,3], OR=11).
For the Shortest Subarray With OR at Least K I coding problem, the sliding window bit manipulation array interview pattern is the approach. The key property: OR is monotonically non-decreasing when adding elements — once a window's OR ≥ K, no extension of that window gives a shorter valid subarray with that starting index. Practice the "OR is monotone" insight — it's the same property that makes the harder version (Part II) tractable with a bit-count-based sliding window. Understanding this monotonicity is the gateway to all bitwise window problems.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Shortest Subarray With OR at Least K II | Medium | Solve | |
| Longest Nice Subarray | Medium | Solve | |
| Smallest Subarrays With Maximum Bitwise OR | Medium | Solve | |
| Alternating Groups I | Easy | Solve | |
| Bitwise OR of Adjacent Elements | Easy | Solve |