## Google Interview Question for Site Reliability Engineers

Team: Site reliabilty
Country: United States
Interview Type: Phone Interview

Comment hidden because of low score. Click to expand.
16
of 16 vote

``````bool is_power_of_2 (int num) {
return ((num & (num-1)) == 0);
}``````

e.g 4 in binary is 100
3 in binary is 011
4&3 000

Comment hidden because of low score. Click to expand.
0

This function won't work for num=0.

Comment hidden because of low score. Click to expand.
1
of 1 vote

It obviously won't work for 0. You can raise 2 to the power of any integer, the answer will never be 0.

Comment hidden because of low score. Click to expand.
0

How about if number is 9 (3 ^ 2)
Binary of 9 : 1001
Binary of 8 : 1000

1001 & 1000 = 1000 (bin) which is 8

Comment hidden because of low score. Click to expand.
0

> How about if number is 9 (3 ^ 2)
The ret val will be false, as 9 is not a power of 2 (but a power of 3)
We are looking for numbers in the form 2^x (x: any unsigned int)

Comment hidden because of low score. Click to expand.
0

> This function won't work for num=0.
Why not?
0 == 00...00b
0-1 ==-1 == 11...11b
00..00b & 11..11b == 0 => return true

Comment hidden because of low score. Click to expand.
0

Comment hidden because of low score. Click to expand.
0

return num&!(num&(num-1))

Comment hidden because of low score. Click to expand.
4
of 4 vote

All integers that is power of 2, contains only one set bit in its bit-representation.
So idea is to check, only one bit is set or not in bit representation of a number.
Below is the code:

``````int isPowerOf2(int n){

return  n && !(n&(n-1));
}``````

this function will return 1, if number n is power of 2.
Else it will return 0;

Comment hidden because of low score. Click to expand.
0
of 0 vote

``return ((num & (num-1))==0);``

Comment hidden because of low score. Click to expand.
0
of 0 vote

return (n&-n)==n

Comment hidden because of low score. Click to expand.
0
of 0 vote

Are you guys confusing "Power" and "Factor"?

Factor: 2 x n
Power: 2^n

IsPower(10) == false
IsFactor(10) == true

Determining power would require walking the binary to ensure only a single bit is set, and that the first bit is not that bit.

Comment hidden because of low score. Click to expand.
0
of 0 vote

Except for bit operation, a naive idea:

``````#include <stdio.h>
#include <stdlib.h>

bool is_power_of_2(int n)
{
if (n == 0) return false;
if (n < 0) n = -n;

int q, m;

do {
q = n / 2;
m = n % 2;
n = q;
} while (m == 0 && q > 0);

return n == 0;
}

int main(int argc, char *argv[])
{
(void)argc;

int n = atoi(argv[1]);

if (is_power_of_2(n)) {
printf("%d is power of 2\n", n);
} else {
printf("%d is not power of 2\n", n);
}

return 0;
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````def is_power_of_two(n):
a = str(bin(n))
if a.count('1')==1:
return True
return False``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

When using the logarithm on the equation num=2^x it is: log(num)=log(2)*x
After rewriting the equation it is: x=log(num)/log(2)
We want to know only num=2^x when x is an integer, so we just need to check for it.

``````public boolean is_power_of_2 (int num) {
double x=Math.log10(num)/Math.log10(2);
if (x==Math.floor(x)) {
return(true);
} else {
return(false);
}
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

When using the logarithm on the equation num=2^x it is: log(num)=log(2)*x
After rewriting the equation it is: x=log(num)/log(2)
We want to know only num=2^x when x is an integer, so we just need to check for it.

``````public boolean is_power_of_2 (int num) {
double x=Math.log10(num)/Math.log10(2);
if (x==Math.floor(x)) {
return(true);
} else {
return(false);
}
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

When using the logarithm on the equation num=2^x it is: log(num)=log(2)*x
After rewriting the equation it is: x=log(num)/log(2)
We want to know only num=2^x when x is an integer, so we just need to check for it.

``````public boolean is_power_of_2 (int num) {
double x=Math.log10(num)/Math.log10(2);
if (x==Math.floor(x)) {
return(true);
} else {
return(false);
}
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

Let's try this:

``````public class IsPowerOf2 {

public static void main(String[] args) {
System.out.println(isPowerOf2(16384));
System.out.println(isPowerOf2(16382));
}

private static boolean isPowerOf2(int i) {
if (i < 2) {
return false;
}
int a = 2;
while (a < i) {
a = 2 * a;
if (i == a) {
return true;
}
}
return false;
}

}``````

Comment hidden because of low score. Click to expand.
-1
of 1 vote

``return (num > 0);``

Yes, I know you meant "integer power of 2", but you might want to be careful with your definitions.

Comment hidden because of low score. Click to expand.
-1
of 1 vote

If a number is a power of 2, then the least significant bit isn't set, so...

``````bool isPowerTwo(int n)
{
return ( ( n & 1 ) == 0 );
}``````

Comment hidden because of low score. Click to expand.
0

Test case: 1 (binary: 0b1; = 2^0)

Doesn't work.

Comment hidden because of low score. Click to expand.
-1
of 1 vote

//How to check if a number is power of 2
public class PowerOfTwo {

public static void main (String [] args) {

int num = 2;

int check = 2;
int powerOf = 0;
int two = 2;

while (true) {
if(num == check ) {
powerOf++;
System.out.println(" " + num + " is 2^" + powerOf);
break;
} else if(num < check) {
System.out.println("Num is not a power of 2");
break;
} else {
check = check * 2;
powerOf++;
}
}
}
}

Comment hidden because of low score. Click to expand.
-1
of 1 vote

``````//How to check if a number is power of 2
public class PowerOfTwo {

public static void main (String [] args) {

int num = 2;

int check = 2;
int powerOf = 0;
int two = 2;

while (true) {
if(num == check ) {
powerOf++;
System.out.println(" " + num + " is 2^" + powerOf);
break;
} else if(num < check) {
System.out.println("Num is not a power of 2");
break;
} else {
check = check * 2;
powerOf++;
}
}
}``````

}

Comment hidden because of low score. Click to expand.
-1
of 1 vote

``````public class PowerOf2 {

public static void main(String[] args) {

int x = Integer.parseInt(args[0]);
do {
if (x%2 != 0)
break;
x =x/2;
}while (x > 1);

if (x ==1)
System.out.println("Yes");
else
System.out.println("No");
}
}``````

Comment hidden because of low score. Click to expand.
-1
of 1 vote

``````public static boolean isPowerOf2(float n) {
if(n == 0) return true;
if(n <=1) return false;
if(n/2 == 1)
return true;
return isPowerOf2(n/2);
}``````

Comment hidden because of low score. Click to expand.
-1
of 1 vote

sum([int(i) for i in bin(number)[2:]]) == 1

Comment hidden because of low score. Click to expand.
-2
of 2 vote

Divide the number with 2 and repeat with the result until the result is even or 1.

Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.