skip to content

Bit Manipulation & Math

Learn the binary layer beneath every integer and the small toolkit of math interviewers expect you to reason about without a library. Interviewers use these topics to probe whether you truly understand how numbers are represented and manipulated — the difference between memorizing a trick and explaining why it works.

part ofData structures & algorithmsoverview, primer and where to startread it →
on this pageshow

explore

questions

page 2 of 2

Why do reviewers reject the temp-free XOR swap of two array slots?

level: middleimportance: should knowfreq 44%

basics

~20 s

Because it silently zeroes the value when both operands are the same storage location — a partition routine that swaps an element with itself destroys it. It is also not faster than a swap through a temporary on modern hardware.

open as a page

Your permissions service outgrows 31 flags in a 32-bit mask — what breaks, and how do you fix it?

level: seniorimportance: should knowfreq 32%

basics

~20 s

Flag 31 lands on the sign bit, so masks turn negative. Past that, shifting by 32 or more is not a reliable zero: many environments reduce the count modulo the width, so flag 32 silently aliases flag 0.

open as a page

Why does the mask expression (1 << bits) - 1 break when bits equals the integer's full width?

level: seniorimportance: should knowfreq 38%

basics

~20 s

A shift count equal to the operand's width is outside what shifts define: languages variously wrap the count, yield zero, trap, or leave it undefined. Build the all-ones mask without shifting by the full width.

open as a page

Is brute-forcing every visit order of 12 delivery stops viable, and how do you know?

level: seniorimportance: should knowfreq 50%

basics

~20 s

Barely, and only offline: 12! = 479,001,600 orderings is a few hundred million evaluations, roughly seconds to minutes on one core. But 13! is 6.2 billion and 15! is 1.3 trillion, so the approach dies within three more stops.

open as a page

For a request counter nearing its fixed-width ceiling, when do you pick checked, saturating or wrapping arithmetic?

level: seniorimportance: should knowfreq 42%

basics

~20 s

Pick by what a wrong number costs. Checked signals and suits values that must be exact; saturating clamps and suits gauges that may be understated; wrapping is correct only for genuinely modular quantities like sequence numbers.

open as a page

How do you compute the gcd of a whole array of package sizes, including zero entries?

level: seniorimportance: should knowfreq 40%

basics

~20 s

Fold pairwise: start an accumulator at 0 and replace it with gcd(accumulator, next size). Zero is the identity, so zero-size entries change nothing, and once the accumulator reaches 1 you can stop early — it can never rise again.

open as a page

A nightly job sieves every prime below 10^7. What dominates its cost, and what breaks at 10^9?

level: seniorimportance: should knowfreq 36%

basics

~20 s

Memory dominates, not time. The sieve keeps one flag per candidate, so a byte-per-flag run costs about ten megabytes at 10^7 but a gigabyte at 10^9, while the near-linear marking work stays affordable. Raising the ceiling breaks the flag array first.

open as a page

When is a bitmask the wrong representation for a permission model your team must maintain for years?

level: principalimportance: should knowfreq 30%

basics

~20 s

A mask wins when the check path is hot and the flag list is stable; it loses when permissions must be read by humans, grow steadily, or are persisted — positions freeze into a contract and capacity is capped.

open as a page

Why mandate decimal or fixed-point money in a billing ledger when floats hold 15 digits?

level: principalimportance: should knowfreq 52%

basics

~20 s

One hundredth is non-terminating in binary, so every amount held in a 64-bit float is approximate from the first assignment and error accumulates across millions of postings. Decimal or integer minor units make amounts exact and rounding an explicit, auditable rule.

open as a page

How do you decide between an XOR fold and a hash set for finding the one unpaired transfer id?

level: principalimportance: should knowfreq 36%

basics

~20 s

Decide on what each buys beyond the answer. The fold runs in constant space and shards trivially, but returns a bare id and fails silently when the exactly-paired invariant breaks. A set costs memory but returns every anomaly with records you can investigate.

open as a page

In submask enumeration, why does s = (s - 1) & m walk exactly the subsets of mask m?

level: middleimportance: nice to knowfreq 18%

basics

~20 s

Subtracting one borrows through the lowest set bit, clearing it and setting all bits below; ANDing with m discards bits m does not own. The result is the next-smaller submask, so the loop descends through all of them.

open as a page

Why does Pascal's rule C(n,k) = C(n-1,k-1) + C(n-1,k) hold combinatorially?

level: middleimportance: nice to knowfreq 34%

basics

~20 s

Fix one element and split every selection by whether it is chosen: those including it pick k-1 more from the remaining n-1, those excluding it pick all k from those n-1. The cases are disjoint and exhaustive, so the counts add.

open as a page

Why can a backwards array scan using an unsigned index never terminate when its loop guard is `i >= 0`?

level: seniorimportance: nice to knowfreq 30%

basics

~20 s

An unsigned integer cannot hold a negative value, so the guard i >= 0 is always true. When i reaches 0, decrementing wraps it to the maximum unsigned value — the bit pattern of -1 — and the scan runs past the array forever.

open as a page

A grid engine counts set bits in a 64-bit occupancy word a billion times a frame — is the clear-lowest-bit loop good enough?

level: seniorimportance: nice to knowfreq 30%

basics

~20 s

No. The loop costs one unpredictable branch per set bit, so its price rises with board density. At that call volume use a fixed-cost population-count instruction, with a branch-free shift-and-mask fallback where the hardware lacks one.

open as a page

Why do 64-bit record identifiers get silently corrupted by a 64-bit-float numeric type?

level: seniorimportance: nice to knowfreq 32%

basics

~20 s

A 64-bit float carries a 53-bit significand, so it represents integers exactly only up to 2^53, about 9.0e15. Larger identifiers round to the nearest representable value with no error raised, so distinct IDs shift by one or collide with each other.

open as a page

A teammate proposes swapping the 1e9+7 modulus for a prime near 1e18. What breaks, and what would you say?

level: seniorimportance: nice to knowfreq 28%

basics

~20 s

Fixed-width multiplication breaks. Two residues below 10^18 multiply to about 10^36, roughly 120 bits, which silently wraps a 64-bit accumulator. The conventional modulus is prime and sits just under 2^30 precisely so the squared product still fits.

open as a page

With XOR, how do you recover both values when two sequence numbers are dropped?

level: seniorimportance: nice to knowfreq 30%

basics

~20 s

One fold gives the combination of the two missing numbers, and any bit set in it marks a position where they disagree. Splitting the whole stream on that bit puts one in each half, so folding each half separately recovers both.

open as a page

showing 31–47 of 47