← L&T Mindtree bank

L&T Mindtree · Question 73

Reverse Bits

Javacode⏱ O(1)💾 O(1)
Service-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 (unsigned), 32 rounds.

Sample input / output

Input:
n = 11111111111111111111111111111101 (i.e. -3 as int)
Output:
-1073741825

Constraints

32-bit unsigned semantics; Java int uses >>> for logical shift.

Solution — read it, then walk it

Time O(1)Space O(1)Loops 1Returns 1source · Java
Scroll sideways for wide charts — the view follows the active step.
↻ repeatSTARTimport java.util.*DEFINE class MainDEFINE static intreverseBits(int n)DEFINE result = 0for (int i = 0; i <32; i++)result <<= 1result |= (n & 1)n >>>= 1RETURN resultDEFINE static voidmain(String[] args)System.out.println(reverseBits(-3))END
  • start / end
  • statement
  • condition
  • loop
  • returns
  • definition
1/12

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