Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on bit manipulation fundamentals.
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 - 2n is even.Follow up: If this function is called many times, how would you optimize it?
Problem summary: 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
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Bit Manipulation
43261596
2147483644
reverse-integer)number-of-1-bits)a-number-after-a-double-reversal)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #190: Reverse Bits
public class Solution {
// you need treat n as an unsigned value
public int reverseBits(int n) {
int ans = 0;
for (int i = 0; i < 32 && n != 0; ++i) {
ans |= (n & 1) << (31 - i);
n >>>= 1;
}
return ans;
}
}
// Accepted solution for LeetCode #190: Reverse Bits
func reverseBits(n uint32) (ans uint32) {
for i := 0; i < 32; i++ {
ans |= (n & 1) << (31 - i)
n >>= 1
}
return
}
# Accepted solution for LeetCode #190: Reverse Bits
class Solution:
def reverseBits(self, n: int) -> int:
ans = 0
for i in range(32):
ans |= (n & 1) << (31 - i)
n >>= 1
return ans
// Accepted solution for LeetCode #190: Reverse Bits
impl Solution {
pub fn reverse_bits(mut n: u32) -> u32 {
let mut ans = 0;
for i in 0..32 {
ans |= (n & 1) << (31 - i);
n >>= 1;
}
ans
}
}
// Accepted solution for LeetCode #190: Reverse Bits
function reverseBits(n: number): number {
let ans = 0;
for (let i = 0; i < 32 && n; ++i) {
ans |= (n & 1) << (31 - i);
n >>= 1;
}
return ans >>> 0;
}
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.