24. Reverse Bits
easyAsked at PlaidReverse the bits of a 32-bit unsigned integer. Plaid asks this as a bitwise-fluency check before harder hash-collision and idempotency-key problems.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Plaid loops.
- Glassdoor (2025)— Plaid systems screen.
- LeetCode Discuss (2026)— Plaid OA — bit-manipulation warm-up.
Problem
Reverse the bits of a given 32-bit unsigned integer. Note: In JavaScript, integers are 32-bit signed under bitwise ops; use unsigned right shift (>>> 0) to interpret the result as unsigned.
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 reversal
Convert to a 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 dodges the bit math. Plaid wants to see the shift loop.
2. Bit-by-bit shift
Pop the LSB of n with (n & 1), push it into result. Shift n right, shift result left. 32 iterations.
- Time
- O(32)
- Space
- O(1)
function reverseBits(n) {
let r = 0;
for (let i = 0; i < 32; i++) {
r = (r << 1) | (n & 1);
n >>>= 1;
}
return r >>> 0;
}Tradeoff: Constant time (32 bits), constant space. The >>> 0 at the end coerces to unsigned for the return value.
Plaid-specific tips
Plaid grades this on whether the shift loop comes naturally. Bonus signal: mention the constant-time mask-and-swap optimization (swap halves, then quarters, etc., in log2(32) = 5 steps). Connect bit reversal to hashing for idempotency keys.
Common mistakes
- Using >> (signed) instead of >>> (unsigned) — corrupts the high bit.
- Forgetting the final >>> 0 — returns a negative number in JS.
- Looping only until n becomes 0 — drops leading zeros, getting the wrong reversed value.
Follow-up questions
An interviewer at Plaid may pivot to one of these next:
- Mask-and-swap in 5 steps (constant-time, no loop).
- Reverse only the lower k bits.
- Count set bits (LC 191).
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why >>> instead of >> in JS?
>> sign-extends — the high bit gets sticky. >>> is logical right shift, zero-filled. For unsigned bit ops, >>> is the safe default.
What's the log-steps optimization?
Swap each pair of bits, then each pair of pairs (4-bit blocks), then 8-bit, 16-bit. Five steps total instead of 32. Useful when called millions of times.
Practice these live with InterviewChamp.AI
Drill Reverse Bits and other Plaid interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →