Google Interview Question
1,069 Interview Reviews |
Back to all Google Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Software Engineer Test at Google:
Onsite Interview 2 a): check whether a number is the power of 2 b) Skyline silhouette puzzle . c) Discussion on uses of hash-tables and trees ? d) Few general questions on Work and academic background .
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (5)
return (a&(a-1)==0);
}
Helpful Answer?
Yes |
No
Inappropriate?
You are expected to give the solution as Anonymous
which by the way can be done in java as well , ( it's generally called bit wise operations)
Helpful Answer?
Yes |
No
Inappropriate?
You are expected to give the solution as Anonymous
which by the way can be done in java as well , ( it's generally called bit wise operations)
Helpful Answer?
Yes |
No
Inappropriate?
You are expected to give the solution as Anonymous
which by the way can be done in java as well , ( it's generally called bit wise operations)
Helpful Answer?
Yes |
No
Inappropriate?
Members can
answer or comment on this question
–
Join Now (It's Free) or
Sign In
0 of 0 people found this helpful
by ellemeno:
Simple solution:
boolean isPowerOfTwo(int n){
double d = (Math.log(n))/(Math.log(2)); // == log(base 2) n
if (d == Math.floor(d)) return true;
return false;
}
Without Java Math class:
boolean isPowerOfTwo(int n){
int x = 1;
while(true){
if (x == n) return true;
if (x > n) return false;
x = x * 2;
}
}
or
boolean isPowerOfTwo(int n){
if (n < 1) return false;
while(n != 1){
if (n % 2 != 0) return false;
n = n/2;
}
return true;
}
Are there any problems with these approaches? What might be a better approach?