Google Interview Question for Software Engineers


Country: Switzerland




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

boolean isPowOfTwo(int n) {
	
	return (n > 0 && (n & (n - 1) == 0)) 
}

be sure to check that n > 0 since negative numbers aren't powers of 2

- SK July 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
5
of 7 vote

I found this solution in the book "Cracking the code interview 5ed" chapter 5 Bit manipulation

((n & (n-1)) == 0)

- hnatsu July 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

and check n is positive

- lxfuhuo July 19, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

public boolean isPowerOfTwo(int n)
{
if(n<=0)
{
return false;
}
n=n & (n-1);
return n==0;
}

- divm01986 July 15, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Simple solution counting the number of bits set:

private static boolean slow(int n) {
        int count = 0;
        for (int i = 0; i < 32; i++) {
            if ((n & 1) == 1) {
                count++;
            }
            n >>= 1;
        }
        return count == 1;
    }

Better solution exploiting the properties of two's complement representation of and integer:

private static boolean fast(int n) {
        return ((n & -n) == n);
    }

- tested.candidate July 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

were you asked all of these questions during a single interview? was it on the phone?

- damluar July 15, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Just wanted to add that (n & (~n+1) ) == n, looks more clear & intuitive.

- JoyZombie August 09, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Actually bit manipulation is way faster than these operations.

- Frank October 16, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This method taken in the number as the parameter and returns whether the number is of the power of two or not.

public static boolean isTwoPower(int num){
while(true){
int q = num/2;
int r = num%2;

if(r != 0){
return false;
}
if(q == 1){
return true;
}
num = q;
}
}

- Infinite Possibilities July 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void powOf2(int i)
{
	int x =1;
	while(x<i)
		x = x*2;
	if(x==i)
		cout << "\n power of 2";
	else
		cout << "\n not power of 2";
	return;
}

- sv July 15, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

if(n>0 && n&(n-1) ==0)
	return true
else
	return false

- codealtecdown July 20, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void powOf2(int number) {

double s = Math.log(number) / Math.log(2);
if ((s-(int)s) != 0)
System.out.println("not power of 2");
else
System.out.println("is power of 2");

}

- Prajwal P July 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void p2(int number) {

double s = Math.log(number) / Math.log(2);
if ((s - (int) s) != 0)
System.out.println("not power of 2");
else
System.out.println("is power of 2");

}

- Prajwal P July 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void p2(int number) {

		double s = Math.log(number) / Math.log(2);
		if ((s - (int) s) != 0)
			System.out.println("not power of 2");
		else
			System.out.println("is power of 2");

	}

- Prajwal P July 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def pow2(n):
if n > 1: return pow2(n/2.0)
return n ==1
Runs in log(n)

- Anonymous August 05, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i=0,n,j;
printf("enter d element:");
scanf("%d",&n);
j=n;
while(n>0)
{
n=n/2;
i++;
};
if(j==pow(2,i-1))
printf("%d is a power of 2",j);
else
printf("%d is not a power of 2",j);
}

- sekhar August 13, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i=0,n,j;
printf("enter d element:");
scanf("%d",&n);
j=n;
while(n>0)
{
n=n/2;
i++;
};
if(j==pow(2,i-1))
printf("%d is a power of 2",j);
else
printf("%d is not a power of 2",j);
}

- sekhar August 13, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void main()
{
int i=0,n,j;
printf("enter d element:");
scanf("%d",&n);
j=n;
while(n>0)
{
n=n/2;
i++;
};
if(j==pow(2,i-1))
printf("%d is a power of 2",j);
else
printf("%d is not a power of 2",j);
}

- sekhar August 13, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Linear solution not using bit manipulation

func isPowerOfTwo(n: Int) -> Bool {
    var multiple = 1
    for _ in 1...n where n%2 == 0 {
        multiple *= 2
        if multiple == n {
            return true
        }
    }
    
    return false
}

- Stephen March 19, 2017 | Flag Reply


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More