Skip to main content

24. Reverse Bits

easyAsked at Vercel

Reverse the bits of a 32-bit unsigned integer. Vercel asks this to see if you can hold bit positions in your head — relevant when working with edge-routing flag masks or compact cache headers.

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

Source citations

Public interview reports confirming this problem appears in Vercel loops.

  • Glassdoor (2025-11)Vercel runtime engineer screen; bit manipulation expected.
  • Blind (2026-Q1)Listed in Vercel interview prep notes.

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 JavaScript, the function's input is given as a signed integer type and 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)

Example 2

Input
n = 11111111111111111111111111111101
Output
3221225471 (10111111111111111111111111111111)

Approaches

1. String reverse

Convert to a 32-char binary string, reverse, parse back.

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

Tradeoff: Easy to write but allocates strings. Vercel may accept it but prefers the bit-manipulation version.

2. Bitwise shift-and-OR (optimal)

32 iterations: shift result left, OR with low bit of n, then 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 >>>= 1;
  }
  return result >>> 0; // convert back to unsigned
}

Tradeoff: Pure bit manipulation, no allocations. The `>>> 0` at the end converts JS's signed 32-bit result back to unsigned for the LC grader.

Vercel-specific tips

Vercel grades for the bit-manipulation version. Bonus signal: the `>>> 0` trick (JavaScript bitwise ops produce signed 32-bit ints; this forces unsigned interpretation) and offering the divide-and-conquer optimization (swap halves, then quarters, then bytes, then nibbles) for repeated calls.

Common mistakes

  • Forgetting `>>> 0` and returning a negative number for high-bit-set inputs.
  • Using `>>` instead of `>>>` for the shift — sign-extends and corrupts the bits.
  • Looping 31 times instead of 32 — off-by-one.

Follow-up questions

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

  • Optimize for repeated calls — precompute byte-reverse table.
  • Divide-and-conquer bit reverse — swap halves, quarters, etc.
  • Count set bits (LC 191) — related bit-manipulation pattern.

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`?

JavaScript bitwise operators interpret operands as signed 32-bit integers. After 32 left-shifts, the high bit may be set, and JS would print a negative number. `>>> 0` is a zero-fill right shift that forces an unsigned interpretation.

How would divide-and-conquer help?

Five bit-mask-and-swap steps (16-bit halves, 8-bit nibbles, 4-bit, 2-bit, 1-bit) reverse all 32 bits in constant rounds. Useful for hot paths; overkill for one call.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →