Power of Two Without Division
Updated
Problem statement
Given a signed integer n, determine whether it is a positive power of two.
A positive integer is a power of two when it can be written as 2^k for an integer k >= 0. Your solution must not use division or a library exponentiation helper such as Math.pow.
Return true when n is a power of two and false otherwise.
Function
isPowerOfTwo(n: int) → booleanExamples
Example 1
n = 16return = true16 = 2^4, so the result is true.
Example 2
n = 18return = false18 is positive but cannot be written as a single power of two.
Example 3
n = 1return = true1 = 2^0, so it is a power of two.
Constraints
-2^31 <= n <= 2^31 - 1.1counts as a power of two.- Every value less than or equal to
0returnsfalse. - The solution must not use division or a library exponentiation helper.
More Arm interview questions
Similar questions at other companies
- Count the 1 Bits in a 32-Bit ValueAmazon · PHONE SCREEN
- Even or Odd Without DivisionAdobe · PHONE SCREENAdobe interview questions
- Powers of Two in a RangeAkuna Capital · PHONE SCREENAkuna Capital interview questions
- Choose the Next Minesweeper ActionRunloop · PHONE SCREEN
- IP Address to CIDR BlocksOpenAI · PHONE SCREENOpenAI interview questions
- Uniform Random Number from Coin TossesPatreon · PHONE SCREENPatreon interview questions
- Fewest Games to Find the Top Two TeamsSusquehanna International Group (SIG) · PHONE SCREENSusquehanna International Group (SIG) interview questions
- IP Range to CIDR BlocksAirbnb · PHONE SCREENAirbnb interview questions
- Minimum Testers to Find the Poisoned BottleWalleye Capital · OA
- Address on Same PageNVIDIA · OANVIDIA interview questions