Question bank

How do you implement a function to determine if a given number is a power of two?

January 31, 2025Updated March 31, 20264 min read
EasyCodingProgrammingProblem-SolvingAlgorithm DesignSoftware EngineerData Scientist
How do you implement a function to determine if a given number is a power of two?

Approach To effectively answer the question "How do you implement a function to determine if a given number is a power of two?" , follow this structured framework: Understand the Problem : Define what it means for a number to be a power of two. Choose a…

Approach

To effectively answer the question "How do you implement a function to determine if a given number is a power of two?", follow this structured framework:

  1. Understand the Problem: Define what it means for a number to be a power of two.
  2. Choose a Method: Explore different methods to solve the problem, such as bitwise operations, logarithms, or iterative division.
  3. Plan the Function: Outline the function's structure, including input, output, and logic.
  4. Implement and Test: Write the code and test it with various inputs.

Key Points

  • Definition: A number is a power of two if it can be expressed as \( 2^n \), where \( n \) is a non-negative integer. For example, 1 (which is \( 2^0 \)), 2 (which is \( 2^1 \)), 4 (which is \( 2^2 \)), 8 (which is \( 2^3 \)), etc.
  • Characteristics:
  • Powers of two in binary representation have exactly one bit set to 1. For instance, \( 4 \) in binary is \( 100 \), \( 8 \) is \( 1000 \).
  • Negative numbers and zero cannot be powers of two.
  • Efficiency: Strive for a solution that runs in constant time \( O(1) \) or logarithmic time \( O(\log n) \).

Standard Response

Here is a sample implementation in Python to determine if a number is a power of two:

def is_power_of_two(n: int) -> bool:
 # Check if n is greater than 0
 if n <= 0:
 return False
 # Check if n is a power of two using bitwise operation
 return (n & (n - 1)) == 0

Explanation of the Code:

  • Input Validation: The function first checks if the number \( n \) is greater than 0. If not, it returns False.
  • Bitwise Operation: The expression (n & (n - 1)) checks if \( n \) has only one bit set. If true, \( n \) is a power of two, and the function returns True.

Testing the Function:

You can test the function with the following cases:

print(is_power_of_two(1)) # True, as 2^0 = 1
print(is_power_of_two(2)) # True, as 2^1 = 2
print(is_power_of_two(3)) # False, as it's not a power of two
print(is_power_of_two(4)) # True, as 2^2 = 4
print(is_power_of_two(0)) # False, as 0 is not a power of two
print(is_power_of_two(-8)) # False, as negative numbers are not powers of two

Tips & Variations

Common Mistakes to Avoid:

  • Ignoring Input Constraints: Failing to handle cases where the input is zero or negative.
  • Incorrect Logic: Using incorrect mathematical checks that may lead to false positives or negatives.

Alternative Ways to Answer:

  • Using Logarithms: Another method to check if a number is a power of two is to use logarithms:
import math

 def is_power_of_two_log(n: int) -> bool:
 if n <= 0:
 return False
 return math.log2(n).is_integer()

Role-Specific Variations:

  • For Technical Positions: Focus on bitwise operations and efficiency, as demonstrated above.
  • For Managerial Roles: Discuss the importance of algorithm efficiency and how it impacts performance.
  • For Creative Roles: Explain the conceptual understanding of powers of two and how it can apply in unique scenarios, such as graphic design or game development.

Follow-Up Questions

  • What are the limitations of your approach?
  • Discuss potential issues with very large integers.
  • Can you explain the time complexity of your solution?
  • Explain that the bitwise method runs in \( O(1) \) time.
  • How would you handle very large numbers in this function?
  • Consider using libraries that support arbitrary-precision arithmetic if needed.

By structuring your answer with clarity and depth, you demonstrate not only your technical capabilities but also your ability to communicate complex ideas effectively. This comprehensive guide equips you with the tools necessary to excel in your technical interviews, boost your career growth, and enhance your job search strategies

VA

Verve AI Editorial Team

Question Bank

Related reads

Explore More Question Bank Entries

Describe a situation where you had to persuade someone who was hesitant to take action. What were their reasons for reluctance, what approach did you use to convince them, and what was the outcome?
February 19, 2025Medium

Describe a situation where you had to persuade someone who was hesitant to take action. What were their reasons for reluctance, what approach did you use to convince them, and what was the outcome?

Approach When faced with the interview question, "Describe a time when you needed to convince someone to do something, but they were reluctant to do it," it's essential to structure your response in a clear, logical manner. Here’s a framework that can guide…

Read answer guide
Can you describe an experience where you successfully collaborated with a team? What factors contributed to the team's effectiveness, and what was your specific role in that success? How did you build relationships with your coworkers during this project?
February 11, 2025Medium

Can you describe an experience where you successfully collaborated with a team? What factors contributed to the team's effectiveness, and what was your specific role in that success? How did you build relationships with your coworkers during this project?

Approach To effectively answer the interview question, "Describe a time when you needed to work as part of a team to get a job done, and the team functioned very effectively," follow this structured framework: Select a Relevant Example : Choose a specific…

Read answer guide
Can you share an experience when you worked with a colleague who was upset but didn't express it? How did you recognize their feelings, what actions did you take, and what was the result?
February 2, 2025Medium

Can you share an experience when you worked with a colleague who was upset but didn't express it? How did you recognize their feelings, what actions did you take, and what was the result?

Approach When answering the interview question, "Describe a time when you needed to work with someone who was upset but not saying so," use the STAR method (Situation, Task, Action, Result) to structure your response effectively. This framework will help you…

Read answer guide
Can you describe a situation where you witnessed unprofessional or unethical behavior in the workplace? What actions did you take in response, and what were the outcomes of those actions? How did you handle any potential consequences?
February 5, 2025Hard

Can you describe a situation where you witnessed unprofessional or unethical behavior in the workplace? What actions did you take in response, and what were the outcomes of those actions? How did you handle any potential consequences?

Approach When preparing to answer the interview question, "Describe a time when you observed others working in an unprofessional/unethical manner," it's essential to use a structured framework. Here’s a step-by-step breakdown: Identify the Situation : Choose…

Read answer guide
Can you share an example of when you gave performance feedback to a colleague? What feedback did you provide, how was it received, and what was the result?
January 22, 2025Medium

Can you share an example of when you gave performance feedback to a colleague? What feedback did you provide, how was it received, and what was the result?

Approach When answering the interview question, "Describe a time when you provided feedback to someone about their performance," follow this structured framework: Situation : Describe the context in which the feedback was given. Task : Explain your role and…

Read answer guide
Can you describe a situation where you recognized a skill gap that impacted your ability to perform a task? What motivated you to address this gap, how did you communicate it, and what was the outcome?
January 23, 2025Medium

Can you describe a situation where you recognized a skill gap that impacted your ability to perform a task? What motivated you to address this gap, how did you communicate it, and what was the outcome?

Approach When faced with the interview question, "Describe a time when you realized you lacked a skill that you needed to do a task," it's essential to have a clear and structured response. Follow these logical steps to craft an effective answer: Identify…

Read answer guide
Can you share an example of a time you identified an opportunity, how you acted on it, and what the outcome was?
January 10, 2025Medium

Can you share an example of a time you identified an opportunity, how you acted on it, and what the outcome was?

Approach To effectively answer the interview question, "Describe a time when you recognized an opportunity and acted on it. What was the opportunity? What did you do? What was the outcome?", follow this structured framework: Identify the Opportunity :…

Read answer guide
Can you provide an example of a time when you realized someone wasn't understanding your message? What actions did you take to clarify it, and how did you confirm their understanding?
January 13, 2025Medium

Can you provide an example of a time when you realized someone wasn't understanding your message? What actions did you take to clarify it, and how did you confirm their understanding?

Approach When responding to the interview question, "Describe a time when you recognized that others weren't grasping what you were saying or writing. What steps did you take to help ensure that your message would be understood? Was it understood? How did…

Read answer guide
Can you share an example of when you advocated for a unique idea that faced opposition? What actions did you take, and what was the result?
January 10, 2025Medium

Can you share an example of when you advocated for a unique idea that faced opposition? What actions did you take, and what was the result?

Approach To effectively answer the interview question, "Describe a time when you supported an idea that no one else supported. What did you do? What was the outcome?", follow this structured framework: Situation : Set the context by describing the scenario.…

Read answer guide