Recognize an Integer Power of Two

Quick Overview

Practice recognizing powers of two across the full signed 32-bit range. Explain the special cases at zero, one, negative values, and numeric boundaries, with an optional loop-free follow-up.

Recognize an Integer Power of Two

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

# Recognize an Integer Power of Two Given an integer `n`, return `true` exactly when there is an integer exponent `x` for which `n = 2^x`. Otherwise return `false`. ### Function `bool isPowerOfTwo(int n)` ### Example 1 ```text Input: n = 64 Output: true ``` Here, `64 = 2^6`. ### Example 2 ```text Input: n = 24 Output: false ``` ### Example 3 ```text Input: n = -8 Output: false ``` ### Constraints - `-2147483648 <= n <= 2147483647`. ### Clarifications The exponent can be zero, so `1` must be accepted. Zero and negative integers are not integer powers of the positive base two. ### What a Strong Answer Covers - A reason the test accepts every permitted power and rejects every other integer. - Correct handling of `1`, `0`, negative inputs, and both signed boundaries. - Numeric safety of any intermediate operation. ### Follow-up Questions Can you solve the task without loops or recursion?

Overview: Practice recognizing powers of two across the full signed 32-bit range. Explain the special cases at zero, one, negative values, and numeric boundaries, with an optional loop-free follow-up.

Google logo
Google
easySoftware EngineerOtherCoding & Algorithms
0
0

Recognize an Integer Power of Two

Given an integer n, return true exactly when there is an integer exponent x for which n = 2^x. Otherwise return false.

Function

bool isPowerOfTwo(int n)

Example 1

Input: n = 64
Output: true

Here, 64 = 2^6.

Example 2

Input: n = 24
Output: false

Example 3

Input: n = -8
Output: false

Constraints

  • -2147483648 <= n <= 2147483647 .

Clarifications

The exponent can be zero, so 1 must be accepted. Zero and negative integers are not integer powers of the positive base two.

What a Strong Answer Covers Guidance

  • A reason the test accepts every permitted power and rejects every other integer.
  • Correct handling of 1 , 0 , negative inputs, and both signed boundaries.
  • Numeric safety of any intermediate operation.

Follow-up Questions Guidance

Can you solve the task without loops or recursion?