Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on bit manipulation fundamentals.
Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values.
Example 1:
Input: n = 5 Output: true Explanation: The binary representation of 5 is: 101
Example 2:
Input: n = 7 Output: false Explanation: The binary representation of 7 is: 111.
Example 3:
Input: n = 11 Output: false Explanation: The binary representation of 11 is: 1011.
Constraints:
1 <= n <= 231 - 1Problem summary: Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Bit Manipulation
5
7
11
number-of-1-bits)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #693: Binary Number with Alternating Bits
class Solution {
public boolean hasAlternatingBits(int n) {
int prev = -1;
while (n != 0) {
int curr = n & 1;
if (prev == curr) {
return false;
}
prev = curr;
n >>= 1;
}
return true;
}
}
// Accepted solution for LeetCode #693: Binary Number with Alternating Bits
func hasAlternatingBits(n int) bool {
prev := -1
for n != 0 {
curr := n & 1
if prev == curr {
return false
}
prev = curr
n >>= 1
}
return true
}
# Accepted solution for LeetCode #693: Binary Number with Alternating Bits
class Solution:
def hasAlternatingBits(self, n: int) -> bool:
prev = -1
while n:
curr = n & 1
if prev == curr:
return False
prev = curr
n >>= 1
return True
// Accepted solution for LeetCode #693: Binary Number with Alternating Bits
impl Solution {
pub fn has_alternating_bits(mut n: i32) -> bool {
let mut prev: i32 = -1;
while n != 0 {
let curr = n & 1;
if prev == curr {
return false;
}
prev = curr;
n >>= 1;
}
true
}
}
// Accepted solution for LeetCode #693: Binary Number with Alternating Bits
function hasAlternatingBits(n: number): boolean {
let prev = -1;
while (n !== 0) {
const curr = n & 1;
if (prev === curr) {
return false;
}
prev = curr;
n >>= 1;
}
return true;
}
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.