Binary Number with Alternating Bits
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 neededProblem
Given a positive integer n, check whether its binary representation (without leading zeros) has alternating bits, meaning every pair of adjacent bits differs.
Return true if the bits alternate, otherwise false.
Try to solve it with a constant number of bitwise operations.
Examples
Input: n = 5
Output: true
5 is 101 in binary.
Input: n = 7
Output: false
7 is 111 in binary.
Input: n = 11
Output: false
11 is 1011 in binary; the last two bits are both 1.
Constraints
- 1 <= n <= 2^31 - 1
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.