Check Adjacent Bits in a Positive Integer

Quick Overview

Check Adjacent Bits in a Positive Integer. Return whether the ordinary binary representation of positive n has different values at every pair of adjacent bits.

Check Adjacent Bits in a Positive Integer

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Return whether the ordinary binary representation of positive n has different values at every pair of adjacent bits. Ignore leading zero padding. ```cpp class Solution { public: bool hasAlternatingBits(int n); }; ``` ### Examples **Example 1:** ```text n = 10 Output: true Explanation: Binary 1010 alternates. ``` **Example 2:** ```text n = 12 Output: false Explanation: Binary 1100 has equal adjacent bits. ``` ### Constraints - The input n is positive and does not exceed 2147483647. ### Clarifying Questions Ignore leading padding and check all adjacent significant bit pairs. ```hint Read the boundary condition What information from the previous extracted bit is sufficient for the next check? ``` ### Contract considerations Ignore leading padding and check all adjacent significant bit pairs.

Overview: Check Adjacent Bits in a Positive Integer. Return whether the ordinary binary representation of positive n has different values at every pair of adjacent bits.

Google logo
Google
Source window: 0–3 months · as of Oct 21, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Return whether the ordinary binary representation of positive n has different values at every pair of adjacent bits. Ignore leading zero padding.

class Solution {
public:
    bool hasAlternatingBits(int n);
};

Examples

Example 1:

n = 10
Output: true
Explanation: Binary 1010 alternates.

Example 2:

n = 12
Output: false
Explanation: Binary 1100 has equal adjacent bits.

Constraints

  • The input n is positive and does not exceed 2147483647.

Clarifying Questions Guidance

Ignore leading padding and check all adjacent significant bit pairs.

Contract considerations

Ignore leading padding and check all adjacent significant bit pairs.