Skip to main content

24. Reverse Bits

easyAsked at Snowflake

Reverse the bits of a 32-bit unsigned integer. Snowflake asks this to test bit-manipulation fluency — relevant for compact column encodings where they sometimes pack rotated/reversed bits to align with vectorized SIMD masks.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Snowflake loops.

  • Glassdoor (2025-Q4)Snowflake storage-team uses this in onsites for low-level encoding discussions.
  • LeetCode Discuss (2025-07)Asked at Snowflake intern screens.

Problem

Reverse bits of a given 32 bits unsigned integer. In JavaScript, the function will only be given as input 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.

Constraints

  • The input must be a binary string of length 32.

Examples

Example 1

Input
n = 00000010100101000001111010011100
Output
964176192 (00111001011110000010100101000000)

Approaches

1. Convert to binary string, reverse, parse

Convert to 32-bit padded binary string; reverse; parse as int.

Time
O(32) = O(1)
Space
O(32)
function reverseBits(n) {
  const bits = n.toString(2).padStart(32, '0');
  const reversed = bits.split('').reverse().join('');
  return parseInt(reversed, 2) >>> 0;
}

Tradeoff: Allocates strings. Works but defeats the purpose.

2. Bit-by-bit shift (optimal for interview)

32 iterations: extract LSB of n, shift left into result.

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;
}

Tradeoff: Pure bit ops. The >>> 0 at the end forces unsigned interpretation in JS.

Snowflake-specific tips

Snowflake interviewers want the bit-by-bit version and to hear you discuss the >>> vs >> distinction in JS. Bonus signal: discuss byte-swap intrinsics (e.g., bswap, vrev) used in their SIMD vectorized kernels for byte-order-sensitive encodings.

Common mistakes

  • Using >> instead of >>> — propagates sign bit, breaks for negative inputs.
  • Forgetting that JS bitwise ops are 32-bit even though Number is 64-bit double.
  • Hardcoding loop bound to 30 or 31 instead of 32.

Follow-up questions

An interviewer at Snowflake may pivot to one of these next:

  • Reverse using a divide-and-conquer mask trick (swap halves, then quarters, then bytes, then nibbles, then pairs).
  • Reverse bits of a 64-bit number.
  • How does Snowflake's columnar encoding use bit-level packing?

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why >>> 0 at the end?

JS's signed-bit interpretation can give negative numbers for high bits. >>> 0 forces unsigned 32-bit interpretation so the return value matches LC's expected format.

Is there a faster way?

Yes — the divide-and-conquer 'swap halves, then quarters, then bytes...' approach does it in log(32) = 5 steps. Worth mentioning but bit-by-bit is what most interviewers expect.

Practice these live with InterviewChamp.AI

Drill Reverse Bits and other Snowflake interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →