Check whether an integer is a power of two
Company: Snapchat
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Onsite
Quick Answer: This question evaluates proficiency in bit-level reasoning and integer arithmetic, specifically understanding binary representations and the properties that define powers of two.
Constraints
- Inputs are Python literals matching the function signature.
- Return a deterministic exact-match value.
Examples
Input: (1,)
Expected Output: True
Explanation: 1 is 2^0.
Input: (16,)
Expected Output: True
Explanation: 16 is a power of two.
Input: (3,)
Expected Output: False
Explanation: 3 has multiple set bits.
Input: (0,)
Expected Output: False
Explanation: Zero is not positive.
Input: (-8,)
Expected Output: False
Explanation: Negative numbers are not powers of two.
Hints
- Clarify edge cases before coding.
- Keep the return value deterministic.