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 pageshowhide
explore
- Bitwise Fundamentals12 questions
- Binary Representation & Two's Complement4 questions
- Bitwise Operators & Shifts4 questions
- Classic Bit Tricks4 questions
- XOR Techniques5 questions
- Bitmasks as Sets & State5 questions
- Number Theory Essentials12 questions
- GCD, LCM & Euclid's Algorithm4 questions
- Primes & the Sieve of Eratosthenes4 questions
- Modular Arithmetic & Overflow-Safe Math4 questions
- Factorials & Combinations4 questions
- Numeric Edge Cases9 questions
- Integer Overflow & Wraparound4 questions
- Floating-Point Comparison Pitfalls5 questions
- Computer Scienceskillanchors this topic
- Data Structures & Algorithmsskillanchors this topic
- AI & Data Scientistrole
- AI Engineerrole
- AI Red Teamingrole
- Android Developerrole
- Backend Developerrole
- Blockchain Developerrole
- Cyber Security Expertrole
- Data Analystrole
- Data Engineerrole
- DevOps / SRE Engineerrole
- DevSecOps Engineerrole
- Forward Deployed Engineerrole
- Frontend Developerrole
- Full Stack Developerrole
- Game Developerrole
- Java Backend Developerrole
- Java SDETrole
- Kotlin Backend Developerrole
- MLOps Engineerrole
- Machine Learning Engineerrole
- Network Engineerrole
- PostgreSQL DBArole
- QA Engineerrole
- Software Architectrole
- iOS Developerrole
questions
page 2 of 2Why do reviewers reject the temp-free XOR swap of two array slots?
basics
~20 sBecause 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.
Your permissions service outgrows 31 flags in a 32-bit mask — what breaks, and how do you fix it?
basics
~20 sFlag 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.
Why does the mask expression (1 << bits) - 1 break when bits equals the integer's full width?
basics
~20 sA 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.
Is brute-forcing every visit order of 12 delivery stops viable, and how do you know?
basics
~20 sBarely, 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.
For a request counter nearing its fixed-width ceiling, when do you pick checked, saturating or wrapping arithmetic?
basics
~20 sPick 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.
How do you compute the gcd of a whole array of package sizes, including zero entries?
basics
~20 sFold 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.
A nightly job sieves every prime below 10^7. What dominates its cost, and what breaks at 10^9?
basics
~20 sMemory 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.
When is a bitmask the wrong representation for a permission model your team must maintain for years?
basics
~20 sA 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.
Why mandate decimal or fixed-point money in a billing ledger when floats hold 15 digits?
basics
~20 sOne 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.
How do you decide between an XOR fold and a hash set for finding the one unpaired transfer id?
basics
~20 sDecide 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.
In submask enumeration, why does s = (s - 1) & m walk exactly the subsets of mask m?
basics
~20 sSubtracting 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.
Why does Pascal's rule C(n,k) = C(n-1,k-1) + C(n-1,k) hold combinatorially?
basics
~20 sFix 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.
Why can a backwards array scan using an unsigned index never terminate when its loop guard is `i >= 0`?
basics
~20 sAn 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.
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?
basics
~20 sNo. 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.
Why do 64-bit record identifiers get silently corrupted by a 64-bit-float numeric type?
basics
~20 sA 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.
A teammate proposes swapping the 1e9+7 modulus for a prime near 1e18. What breaks, and what would you say?
basics
~20 sFixed-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.
With XOR, how do you recover both values when two sequence numbers are dropped?
basics
~20 sOne 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.
showing 31–47 of 47