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.
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
Given two integers x and y, return the Hamming distance between them.
Example 1:
Input: x = 1, y = 4
Output: 2
Explanation:
1 (0 0 0 1)
4 (0 1 0 0)
↑ ↑
The above arrows point to positions where the corresponding bits are different.
Example 2:
Input: x = 3, y = 1 Output: 1
Constraints:
0 <= x, y <= 231 - 1Note: This question is the same as 2220: Minimum Bit Flips to Convert Number.
Problem summary: The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Given two integers x and y, return the Hamming distance between them.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Bit Manipulation
1 4
3 1
number-of-1-bits)total-hamming-distance)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #461: Hamming Distance
class Solution {
public int hammingDistance(int x, int y) {
return Integer.bitCount(x ^ y);
}
}
// Accepted solution for LeetCode #461: Hamming Distance
func hammingDistance(x int, y int) int {
return bits.OnesCount(uint(x ^ y))
}
# Accepted solution for LeetCode #461: Hamming Distance
class Solution:
def hammingDistance(self, x: int, y: int) -> int:
return (x ^ y).bit_count()
// Accepted solution for LeetCode #461: Hamming Distance
struct Solution;
impl Solution {
fn hamming_distance(x: i32, y: i32) -> i32 {
let mut z = x ^ y;
let mut sum = 0;
for _ in 0..32 {
sum += z & 1;
z >>= 1;
}
sum
}
}
#[test]
fn test() {
assert_eq!(Solution::hamming_distance(1, 4), 2);
}
// Accepted solution for LeetCode #461: Hamming Distance
function hammingDistance(x: number, y: number): number {
x ^= y;
let ans = 0;
while (x) {
x -= x & -x;
++ans;
}
return ans;
}
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.