Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Power of Two Check

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Power of Two Check. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Given an integer n, determine whether it is a power of 2. A number is a power of 2 if it can be written as 2^k for some integer k >= 0.

Implement a function that returns True if n is a power of 2, and False otherwise.

Formal Specification

  • Input: A single integer n
  • Output: A boolean value
  • Return True only when n is a positive power of 2.

Constraints

  • -2^31 <= n <= 2^31 - 1
  • n may be zero or negative
  • Return True only for positive powers of two

Function Signature

def is_power_of_two(n):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output