Smallest Integer Greater Than p With No Consecutive 1s in Binary

Quick Overview

Given a non-negative integer p, find the smallest integer strictly greater than p whose binary representation has no two adjacent 1 bits. The problem tests bit manipulation, reasoning about binary carries, and finding a search fast enough for inputs up to one billion, along with an argument that it stays correct.

Smallest Integer Greater Than p With No Consecutive 1s in Binary

Company: Airbnb

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given a non-negative integer `p`, find the smallest integer greater than `p` whose binary representation does not contain the substring `"11"` (two consecutive `1`s). For example, the number `6` in binary is `"110"`, which contains `"11"`. The number `5` in binary is `"101"`, which does not contain `"11"`. Return the smallest integer strictly greater than `p` that satisfies this condition. ### Function Signature ```python def smallest_without_consecutive_ones(p: int) -> int: ``` ### Rules - The binary representation is the standard base-2 form without leading zeros. - The answer must be strictly greater than `p`, even when `p` itself has no `"11"`. For `p = 5`, the answer is 8, because 6 (`110`) and 7 (`111`) both contain `"11"`. ### Constraints - `0 <= p <= 10^9` ### Examples ```text Input: p = 6 Output: 8 ``` The only candidate below 8 is 7, which is `111` in binary and contains `"11"`. 8 is `1000`, which has no two adjacent 1s. ```text Input: p = 4 Output: 5 ``` 5 is `101` in binary, which has no two adjacent 1s, so the first candidate already works. ```text Input: p = 10 Output: 16 ``` 11 (`1011`), 12 (`1100`), 13 (`1101`), 14 (`1110`) and 15 (`1111`) all contain `"11"`. 16 is `10000`, the first value above 10 that does not.

Overview: Given a non-negative integer p, find the smallest integer strictly greater than p whose binary representation has no two adjacent 1 bits. The problem tests bit manipulation, reasoning about binary carries, and finding a search fast enough for inputs up to one billion, along with an argument that it stays correct.

Airbnb logo
Airbnb
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given a non-negative integer p, find the smallest integer greater than p whose binary representation does not contain the substring "11" (two consecutive 1s).

For example, the number 6 in binary is "110", which contains "11". The number 5 in binary is "101", which does not contain "11".

Return the smallest integer strictly greater than p that satisfies this condition.

Function Signature

def smallest_without_consecutive_ones(p: int) -> int:

Rules

  • The binary representation is the standard base-2 form without leading zeros.
  • The answer must be strictly greater than p , even when p itself has no "11" . For p = 5 , the answer is 8, because 6 ( 110 ) and 7 ( 111 ) both contain "11" .

Constraints

  • 0 <= p <= 10^9

Examples

Input: p = 6
Output: 8

The only candidate below 8 is 7, which is 111 in binary and contains "11". 8 is 1000, which has no two adjacent 1s.

Input: p = 4
Output: 5

5 is 101 in binary, which has no two adjacent 1s, so the first candidate already works.

Input: p = 10
Output: 16

11 (1011), 12 (1100), 13 (1101), 14 (1110) and 15 (1111) all contain "11". 16 is 10000, the first value above 10 that does not.