← Meta (Facebook) bank

Meta (Facebook) · Question 74

Power of Two

Pythoncode⏱ O(1)💾 O(1)
Product-based

Problem

You are given integer n. Check if it is a power of two.

Approach

A power of two has exactly one bit set: n & (n-1) clears it to zero.

Time O(1)Space O(1)

How it works

  1. 1Positive numbers only.
  2. 2One set bit clears to exactly zero.

Sample input / output

Input:
16
Output:
True

Constraints

-2^31 <= n < 2^31.

Solution — read it, then walk it

Time O(1)Space O(1)Branches 1Returns 1source · Python
Scroll sideways for wide charts — the view follows the active step.
yesnoSTARTnRETURN n > 0 and (n & (n -1)) == 0DEFINE main()import sysdata =sys.stdin.read().split()it = iter(data)n = int(next(it))print(isPowerOfTwo(n))if __name__ =="__main__"main()END
  • start / end
  • statement
  • condition
  • loop
  • returns
  • definition
1/11

StartSTART

Interactive control-flow diagram and step-by-step code walkthrough.

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits