Checking if a Number is a Power of Two
Check if number is power of two is an easy quant interview question on Algorithms.
This question focuses on recognizing when an integer has a special structure in its binary form. The candidate is asked to decide whether a number is an exact power of two, using an approach that is efficient and robust across typical integer ranges. This kind of check appears frequently in low-level systems code, performance-critical libraries, and basic building blocks of algorithmic trading infrastructure, where data sizes, buffer lengths, or hashing tables are often constrained to powers of two for efficiency.
To answer well, a candidate needs to understand how binary representations behave under simple arithmetic and bitwise operations, and how that connects to the definition of powers of two. It leans on comfort with bit operations, constant-time checks, and edge-case handling for small, zero, or negative inputs. Interviewers watch for clarity about why a particular test works, not just memorization of a bit trick, and for awareness of implementation pitfalls such as integer overflow or language-specific behavior.
What it tests
The class of problems here is about recognizing numbers with a unique structural property in their binary representation. Powers of two are characterized by having exactly one bit set to 1, and all other bits set to 0. This is because multiplying by two in binary shifts the single 1-bit leftward, never introducing a second 1. The key insight is that subtracting 1 from such a number flips all the bits after the 1, so the original number and its predecessor have no 1-bits in common. This property is not just a curiosity—it is a direct result of how binary numbers increment and how carries propagate when subtracting 1.
Practise this question with written feedback, or hear it in a spoken mock interview.
Get started free