25. Reverse Bits
easyAsked at RedditReverse the bits of a 32-bit unsigned integer. Reddit uses this to test bitwise fluency — the same low-level operation used in their fingerprint-hash byte-swapping (some browsers send hashes in opposite endianness).
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Reddit loops.
- Glassdoor (2026-Q1)— Reddit infra-team phone screen, used to gauge bitwise comfort.
Problem
Reverse bits of a given 32 bits unsigned integer. Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer's internal binary representation is the same, whether it is signed or unsigned. In Java, the compiler represents the signed integers using 2's complement notation.
Constraints
The input must be a binary string of length 32.
Examples
Example 1
n = 00000010100101000001111010011100964176192 (00111001011110000010100101000000)Example 2
n = 111111111111111111111111111111013221225471 (10111111111111111111111111111111)Approaches
1. String reverse
Convert to 32-char binary string, reverse, parse back.
- Time
- O(32)
- Space
- O(32)
function reverseBits(n) {
return parseInt(n.toString(2).padStart(32, '0').split('').reverse().join(''), 2);
}Tradeoff: Works but feels like cheating in an interview that's testing bit ops.
2. Bit-by-bit shift (optimal)
32 iterations: shift result left, OR with low bit of n, shift n right.
- Time
- O(32)
- Space
- O(1)
function reverseBits(n) {
let result = 0;
for (let i = 0; i < 32; i++) {
result = (result << 1) | (n & 1);
n >>>= 1;
}
return result >>> 0;
}Tradeoff: Constant time and space. The >>> 0 at the end coerces back to unsigned 32-bit.
Reddit-specific tips
Reddit interviewers want the bitwise version with no strings. Bonus signal: mention the divide-and-conquer trick (swap halves, then quarters, then bytes) for an O(log 32) version. Mention that >>> in JS is unsigned shift, critical here.
Common mistakes
- Using >> instead of >>> — sign-extends and breaks for n >= 2^31.
- Forgetting the final >>> 0 — returns negative numbers in JS.
- Mixing up bit positions (i should iterate 32, not 31).
Follow-up questions
An interviewer at Reddit may pivot to one of these next:
- Number of 1 bits (LC 191).
- Divide-and-conquer in log time.
- How would you do this if the integer could be 64-bit? (Use BigInt.)
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why >>> instead of >> in JavaScript?
>> is arithmetic (sign-extends); >>> is logical (zero-fills). For unsigned arithmetic on a 32-bit value, >>> is required.
Could this be done faster?
Yes — the bit-pair-swap (0x5555...) then quad-swap etc. gives O(log 32) bit-ops. For a single integer it's not a real speedup.
Practice these live with InterviewChamp.AI
Drill Reverse Bits and other Reddit interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →