Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #190: Reverse Bits

In this guide, we solve Leetcode #190 Reverse Bits in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

Reverse bits of a given 32 bits signed integer. Example 1: Input: n = 43261596 Output: 964176192 Explanation: Integer Binary 43261596 00000010100101000001111010011100 964176192 00111001011110000010100101000000 Example 2: Input: n = 2147483644 Output: 1073741822 Explanation: Integer Binary 2147483644 01111111111111111111111111111100 1073741822 00111111111111111111111111111110 Constraints: 0 <= n <= 231 - 2 n is even.

Quick Facts

  • Difficulty: Easy
  • Premium: No
  • Tags: Bit Manipulation, Divide and Conquer

Intuition

The problem structure lets us track state with bitwise operations.

Bit operations are constant time and avoid extra memory.

Approach

Apply XOR/AND/OR and shifts to maintain the required invariant.

Aggregate the result in a single pass.

Steps:

  • Identify a bitwise invariant.
  • Combine values with bit operations.
  • Return the aggregated result.

Python Solution

class Solution: def reverseBits(self, n: int) -> int: ans = 0 for i in range(32): ans |= (n & 1) << (31 - i) n >>= 1 return ans

Complexity

The time complexity is O(log⁡n)O(\log n)O(logn), and the space complexity is O(1)O(1)O(1). The space complexity is O(1)O(1)O(1).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy