← Meta (Facebook) bank

Meta (Facebook) · Question 73

Reverse Bits

Pythoncode⏱ O(1)💾 O(1)
Product-based

Problem

You are given 32-bit unsigned integer. Reverse its bits.

Approach

Shift bits from n into the result, LSB first, 32 times.

Time O(1)Space O(1)

How it works

  1. 1Pull the lowest bit of n into the result's bottom.
  2. 2Shift result left and n right, 32 rounds.

Sample input / output

Input:
43261596
Output:
964176192

Constraints

0 <= n < 2^32.

Solution — read it, then walk it

Time O(1)Space O(1)Loops 1Branches 1Returns 1source · Python
Scroll sideways for wide charts — the view follows the active step.
↻ repeatyesnoSTARTnresult = 0for i in range(32)result <<= 1result |= (n & 1)n >>= 1RETURN resultDEFINE main()import sysdata =sys.stdin.read().split()it = iter(data)n = int(next(it))print(reverseBits(n))if __name__ =="__main__"main()END
  • start / end
  • statement
  • condition
  • loop
  • returns
  • definition
1/16

StartSTART

Interactive control-flow diagram and step-by-step code walkthrough.

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits