25. Reverse Bits
easyAsked at WorkdayReverse the bits of a 32-bit unsigned integer. Workday uses this to test bitwise fluency — needed when packing role/permission flags into compact bitmaps for RBAC.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Workday loops.
- Glassdoor (2025)— Workday RBAC team — bitwise warmup.
Problem
Reverse bits of a given 32 bits unsigned integer.
Constraints
The input must be a binary string of length 32.Follow-up: If this function is called many times, how would you optimize it?
Examples
Example 1
n = 00000010100101000001111010011100964176192 (00111001011110000010100101000000)Example 2
n = 111111111111111111111111111111013221225471 (10111111111111111111111111111111)Approaches
1. String reverse
Convert to binary string, pad to 32, reverse, parse.
- Time
- O(32)
- Space
- O(32)
return parseInt(n.toString(2).padStart(32,'0').split('').reverse().join(''), 2);Tradeoff: Works but slow and string-heavy. Demo only.
2. Bit shift accumulate
For 32 iterations: shift result left, OR in n's lowest bit, shift n right.
- Time
- O(32) = O(1)
- Space
- O(1)
function reverseBits(n) {
let result = 0;
for (let i = 0; i < 32; i++) {
result = (result << 1) | (n & 1);
n = n >>> 1;
}
return result >>> 0; // force unsigned
}Tradeoff: Constant time. The >>> 0 at the end forces JS to interpret as unsigned 32-bit.
Workday-specific tips
Workday grades the >>> 0 unsigned-cast at the end — JS bitwise ops are signed 32-bit by default, so without it you can return a negative number on inputs with the top bit set. Mention the RBAC analogy.
Common mistakes
- Using >> instead of >>> for n — leaks sign bit into the high bits.
- Forgetting >>> 0 at the end — returns a signed value.
- Looping a variable number of times instead of exactly 32 — leading zeroes are part of the input.
Follow-up questions
An interviewer at Workday may pivot to one of these next:
- Cache 8-bit reversal in a lookup table — process 4 bytes per call.
- Number of 1 Bits (LC 191).
- Bitwise AND of Numbers Range (LC 201).
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why >>> instead of >>?
>>> is unsigned (logical) right shift — fills with 0s. >> is arithmetic — fills with the sign bit. For 32-bit reversal we want pure logical.
How would I optimize for many calls?
Precompute an 8-bit reversal table (256 entries). Then reverse 4 bytes per call with three lookups and shifts. Constant time but cache-friendly.
Practice these live with InterviewChamp.AI
Drill Reverse Bits and other Workday interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →