Coding Track

Blind 75 Bit Manipulation And Math

Practice the smaller Blind 75 category that still trips up experienced candidates: XOR invariants, bit masks, integer boundaries, carry logic, counting bits, and clean explanations under pressure.

Interview Method

Name The Identity Before Coding

Bit problems are short only after the invariant is clear. Before coding, say which identity you are using and how Python's integer behavior differs from fixed-width interview languages.

  1. XOR: x ^ x = 0, x ^ 0 = x, and XOR is commutative.
  2. Last bit: n & 1 checks oddness; n & (n - 1) removes the lowest set bit.
  3. Shifts: right shift divides unsigned values by two; left shift multiplies by powers of two.
  4. Masks: use masks to simulate fixed-width integers when Python's unlimited sign bits would keep extending.
  5. Tests: include zero, one, powers of two, all bits set, negative numbers if allowed, and overflow-like boundaries.
Question: What should you ask before a bit manipulation problem?

Ask whether inputs are signed or unsigned, what integer width to assume, whether overflow wraps, and whether the language is fixed width. In Python, negative integers have conceptually infinite sign bits, so problems like Sum of Two Integers need an explicit mask.

Bit Problems

XOR, Masks, And Popcount

Problem: Number Of 1 Bits

Return the number of set bits in an unsigned integer.

Hidden answer: strategy, invariant, mistakes, tests

Use n & (n - 1) to remove the lowest set bit until n is zero. Invariant: after each loop, the answer counts exactly the set bits removed so far. Mistake to avoid: looping while shifting a negative value if the input is signed. Test zero, one, a power of two, all bits set, and alternating bits.

Problem: Counting Bits

For every value from 0 to n, return its set-bit count.

Hidden answer: strategy, invariant, mistakes, tests

Use bits[i] = bits[i >> 1] + (i & 1) or bits[i] = bits[i & (i - 1)] + 1. Invariant: each state depends on a smaller number already computed. Mistake to avoid: recomputing popcount from scratch for every value. Test n equal to 0, 1, a power of two, and one less than a power of two.

Problem: Reverse Bits

Reverse the bits of a 32-bit unsigned integer.

Hidden answer: strategy, invariant, mistakes, tests

Build the result one bit at a time: shift result left, add the current low bit, then shift input right. Invariant: after k iterations, the result holds the reversed order of the lowest k input bits. Mistake to avoid: stopping early; leading zero bits must be preserved for a 32-bit reverse.

Problem: Missing Number

Given n distinct numbers from 0 to n, return the missing value.

Hidden answer: strategy, invariant, mistakes, tests

XOR all indices, values, and n, or subtract sums. XOR avoids overflow in fixed-width languages. Invariant: every matched index-value pair cancels, leaving only the missing number. Test missing zero, missing n, one-element input, and shuffled input.

Problem: Sum Of Two Integers

Return a + b without using the plus or minus operators.

Hidden answer: strategy, invariant, mistakes, tests

XOR gives the sum without carry; AND shifted left gives the carry. Repeat until carry is zero. In Python, apply a 32-bit mask and convert back from two's complement if the sign bit is set. Test positive plus positive, positive plus negative, negative plus negative, zero, and carry chains such as 7 + 1.

Math Problems

Products, Carries, And Dynamic Counts

Problem: Climbing Stairs

Count ways to climb n stairs taking one or two steps at a time.

Hidden answer: strategy, invariant, mistakes, tests

This is Fibonacci DP. Invariant: prev and cur hold ways for the previous two stair counts. Mistake to avoid: exponential recursion without memoization. Test n equal to 1, 2, 3, and a larger value.

Problem: Multiply Strings

Multiply two non-negative integers represented as strings.

Hidden answer: strategy, invariant, mistakes, tests

Simulate grade-school multiplication into an array of length m + n. Invariant: each digit product contributes to exactly one base position plus carry. Mistake to avoid: converting the whole input to an integer if the prompt forbids it. Test zeros, single digits, carry chains, and different lengths.

Problem: Encode And Decode Strings

Design a reversible encoding for a list of strings.

Hidden answer: strategy, invariant, mistakes, tests

Prefix each string with its length and a delimiter, such as 4#test. Invariant: decoder reads exactly the declared number of characters after each delimiter. Mistake to avoid: joining with a delimiter that may appear inside the data. Test empty list, empty string, delimiter characters, and Unicode if allowed.

Worked Python

Short Implementations With The Invariant Visible

Worked: Counting Bits

Hidden answer: Python solution
def count_bits(n):
    bits = [0] * (n + 1)
    for i in range(1, n + 1):
        bits[i] = bits[i >> 1] + (i & 1)
    return bits

Worked: Reverse Bits

Hidden answer: Python solution
def reverse_bits(n):
    out = 0
    for _ in range(32):
        out = (out << 1) | (n & 1)
        n >>= 1
    return out

Worked: Sum Of Two Integers

Hidden answer: Python solution
def get_sum(a, b):
    mask = 0xffffffff
    max_int = 0x7fffffff
    while b:
        a, b = (a ^ b) & mask, ((a & b) << 1) & mask
    return a if a <= max_int else ~(a ^ mask)

Worked: Encode And Decode Strings

Hidden answer: Python solution
def encode(strings):
    return "".join(f"{len(s)}#{s}" for s in strings)

def decode(blob):
    out = []
    i = 0
    while i < len(blob):
        j = blob.index("#", i)
        size = int(blob[i:j])
        start = j + 1
        out.append(blob[start:start + size])
        i = start + size
    return out

Production Follow-Ups

Connect Bit Thinking To Speech Systems

Follow-Up: Feature Flags For Model Serving

You store serving capabilities in a bit mask: streaming ASR, batch ASR, TTS, retrieval, quantized model, and safety filter. How do you test routing rules?

Hidden answer: routing tests

Test exact capability matches, missing required bits, extra allowed bits, zero mask, disabled safety bit, quantized fallback, and rollout masks per tenant. The invariant is that a request may route only when all required capability bits are present.

Follow-Up: Hashing And Deduplication

A labeling pipeline sees duplicate aggregate review candidates. What should be deduplicated, and what should not be stored?

Hidden answer: privacy-safe answer

Deduplicate by approved metadata, stable fixture IDs, or privacy reviewed hashes. Do not store raw audio, private transcripts, or reversible identifiers unless explicitly allowed by policy. Keep lineage for why a candidate was selected.