The Number of Subarrays With AND Value of K problem asks you to count subarrays where the bitwise AND of all elements equals exactly k. This hard coding problem leverages the property that AND can only decrease or stay the same as the window grows, enabling efficient enumeration of distinct AND values.
DE Shaw asks this because it requires the non-trivial observation that for any fixed right endpoint, the number of distinct AND values of subarrays ending there is O(log(max_value)). This allows efficient enumeration using a "compressed AND value tracking" technique. The array, binary search, bit manipulation, and segment tree interview pattern is the core.
AND compression with right endpoint scan. For each right endpoint j, maintain a set of (AND_value, leftmost_start) pairs for all subarrays ending at j. When extending to j+1: AND each existing value with nums[j+1] and merge equal values (taking the minimum start). Since AND can only decrease or stay, distinct values are O(log max_val). For each entry with AND=k, count the valid start positions.
nums=[1,1,2,3], k=1.
AND/OR subarray counting problems exploit the O(log max_val) distinct values property: AND can only decrease as the window grows. This "at most O(log max_val) distinct values" insight applies to XOR, OR, and AND operations on subarrays with fixed endpoints. Practice problems involving "count subarrays with OR/AND/XOR equal to k" — they all use this compressed distinct-values technique from a fixed endpoint.