Number Complement

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 complement of a positive integer flips every bit in its binary representation **without leading zeros** — every 0 becomes 1 and every 1 becomes 0. Given a positive integer num, return its complement. For example, 5 is 101, which flips to 010, i.e. 2. Only the bits up to and including the highest set bit of num are flipped.

Examples

Input: num = 5
Output: 2
101 → 010 = 2.
Input: num = 1
Output: 0
1 → 0.

Constraints

  • 1 <= num < 2^31
  • Leading zeros are not flipped.
  • The result is non-negative.

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