← TCS bank

TCS · Question 3

Power of Two Check

C++code⏱ O(1)💾 O(1)
Service-based

Problem

Check if a number is a power of two.

Approach

A power of two has exactly one bit set.

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:
n = 16
Output:
true

Constraints

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

Solution — read it, then walk it

Time O(1)Space O(1)Returns 1source · C++
Scroll sideways for wide charts — the view follows the active step.
STARTinclude <iostream>using namespace stdbool isPowerOfTwo(int n)DEFINE main()cout << boolalpha <<isPowerOfTwo(16) << endlRETURN 0END
  • start / end
  • statement
  • condition
  • loop
  • returns
  • definition
1/7

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