Hamming Distance

EasyBit Manipulationbit manipulation

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

The Hamming distance between two non-negative integers is the number of bit positions at which their binary representations differ. Given integers x and y, return their Hamming distance. Use bitwise operations rather than string conversion.

Examples

Input: x = 1, y = 4
Output: 2
1 is 001 and 4 is 100; bits 0 and 2 differ.
Input: x = 3, y = 1
Output: 1
11 vs 01 differ only in bit 1.

Constraints

  • 0 <= x, y <= 2^31 - 1
  • Compare the 31-bit binary forms (leading zeros are equal).
  • The result is in the range [0, 31].

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More bit manipulation problems