Given a binary string composed of '0', '1', and '?' characters, and a lookup table f that maps each 3-bit binary number (from 0 to 7) too either 0 or 1, determine the number of ways to replace all '?' characters with '0' or '1' such that the resulting string can be reduced to "1" using the...
A common interview problem involves counting the number of 1 bits (set bits) in the binary representation of an integer. The key insight lies in understanding how computers store signed integers using two's complement notation. The expression n &= (n - 1) is a powerful optimization that eliminat...
Computing XOR Over an Arithmetic Sequence Given two integers n and start, an array is constructed using the rule arr[i] = start + 2 * i for zero-based indices, where n equals the length. The task is to return the bitwise XOR over all the elements. A straightforward solution iterates through the rang...
Prerequisites Basic understanding of dynamic programming Proficiency with binary bitwise operations Approach This is a classic bitmask dynamic programming problem. Define dp[i][curr_idx] as the number of valid placement schemes after processing the first i rows, where the curr_idx-th precomputed val...
Handling Massive Datasets Identifying Missing Integers in a 4-Billion-Element File with 1GB Memory The range of a 32-bit unsigned integer spans 0 to 4,294,967,295. A file containing 4 billion such integers guarantees some numbers are missing. To find all missing values using 1GB of RAM, construct a...
When evaluating __builtin_clz with an input of 0, the output often matches the result of passing 1 (yielding 31 on a 32-bit integer), which seems counterintuitive. According to the GCC documentation, the result of __builtin_clz is explicitly undefined when the enput is 0: Returns the number of leadi...