Non-negative Integers without Consecutive Ones
HardDynamic Programmingbit 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, count the integers x with 0 <= x <= n whose binary representation contains **no two adjacent 1 bits**.
For example 5 is 101 (allowed) but 3 is 11 (not allowed).
n can be up to 10^9, so checking every integer is too slow. Aim for O(log n) time.
Examples
Input: n = 5
Output: 5
Of 0, 1, 10, 11, 100, 101 only 11 (which is 3) has adjacent ones, leaving 5 numbers.
Input: n = 1
Output: 2
Both 0 and 1 qualify.
Input: n = 2
Output: 3
0, 1 and 10 all qualify.
Constraints
- 1 <= n <= 10^9
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.