Google Interview Question for Software Engineer / Developers


Country: United States




Comment hidden because of low score. Click to expand.
14
of 18 vote

Here.

bool isPowerof4(int i) {
    return ((i & 0xAAAAAAAA) == 0 && (i&(i-1)) == 0);
}

- Forest October 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
3
of 3 votes

Haha that's cool!!! Everyone should +1.

That might let 0 through, so you might need to do it like this:

bool isPowerof4(unsigned int n) {
    return ( n !=0U && (n & 0xAAAAAAAAU) == 0 && (n&(n-1)) == 0);
}

- S O U N D W A V E October 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

It is O(lglgn) n=the number

Constant time function is element of O(lglgn) class of functions.

Do we want theta( lglgn ) ?

Just do two level tower of powers 4^(2^i) <--- that's 2 levels of left shifting

Loop on i until some 4^(2^i) is greater than your number (you can make this platform independent, and even work on mythical infinite sized integers)
.
Then you know the number is between 4^(2^(i-1)) and 4^(2^i) for last seen i
{Except some boundary case(s)}

Now binary search on that range. Lower = 2^(i-1), Upper= 2^i

We don't have to keep calculating powers throughout all this, as we can adjust previous powers whenever we have a loop.

And every ^ you see above is some kind of shifting.

- S O U N D W A V E October 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

May be its a very basic question, but what does this represent? 0xAAAAAAAA

- AVK October 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It's a mask having all bits at even positions set.

- joe kidd October 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

a similar solution:

return (((i&0x55555555) & (i&0x55555555 - 0x1)) == 0x0);

- david86391 October 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

This does not work for isPowerof4(1)....

- bluemoon January 18, 2014 | Flag
Comment hidden because of low score. Click to expand.
4
of 4 vote

First check whether number is power of 2:

x = 2^k
x         == 0...010...0
x-1       == 0...001...1
x & (x-1) == 0...000...0

x != 2^k
x         == 0...1...010...0
x-1       == 0...1...001...1
x & (x-1) == 0...1...000...0

If it is not power of 2 - then it is not power of 4.
If it power of 2 - then it have exactly one 1 in binary record.

1   = 000000001 
4   = 000000100 
16  = 000010000 
64  = 001000000 
256 = 100000000

If it is power of 4 - position of this single 1 will be odd.

bool isPowerOfTwo = x && !(x & (x - 1));
//101010100 = 0x5...554
int mask = 0x5...554; //(or 0x5...555 is 1 should be included as 4^0) 
return isPowerOfTwo  && (x & mask );

- Darida October 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 3 vote

Similar to joe_kidd's solution, the pattern we want to find is that there is exactly only one '1' in an odd position of the bit representation of the number.

Instead of looping and checking all possible numbers, which gives O(log n) runtime, we can do binary selection to further optimize it to O(log log n).

The idea is to break the number into 2 parts, and do zero-check on both parts. go to the part that is non-zero, until you find a '1' and check if its position is on an odd index. Go to the original number, flip the 1 on the index you found, and do zero-check of the flipped number.

PS: 1 is also counted as power of 4, I assume

- Zoro October 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I don't get it. n is the bit length of the number? For 100 = b1100100, n=7?

- Mark October 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Is this really O(lglgn)?

"Do zero check on both parts" ? Except for the case when the parts are perfectly aligned and CPU accessible objects (e.g., int, short, char), how would you do that part when you are dealing with parts that are < 1byte in size?

Pseudocode please?

Why not (for sizeof(int) independent alg.) just sweep through the bytes and do "zero check" and "odd 1 set check" byte by byte?

- S O U N D W A V E October 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is not really in log log n. Checking if a value is 0 is a linear check. Think of an array of bits. And I don't like the bit mask solutions. Those are really O(1) if we fix the size of the input. I don't think that's possible. In log log n we can't even see all bits.

- Kuba November 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Oh this one looked cool.
Write a multibyte example on paper, then you get the idea to use a lookup table:

Note: 4^i = (2^2)^i = 2^(2i)

Also, on paper you see that any integer is a power of 4 precisely when:
1) One of it's bytes is a power of 4
2) All other bytes are fully 0

bool LUT[1U<<8];  //look up: is a byte sized integer a power of 4?

//initialize: LUT[1U] = LUT[1U<<2] = LUT[1U<<4] = LUT[1U<<6] = true;

bool ispower4 ( uint x )
{
    union {
        uint whole;
        unsigned char part[sizeof(uint)];
    } var;

    bool bytecheck = false;   //whether we've seen a byte in the integer that's power of 4
    int zerobytes = 0;   //count number of all-0 bytes in integer
    int i;
    
    var.whole=x;
    for(i=0; i < sizeof(uint); i++)
    {
        if( LUT[var.part[i]] ) bytecheck=true;        //i-th byte is a power of 4
        else if (var.part[i] == 0x0 ) zerobytes++;  //i-th byte is all 0s
    }
           //all bytes are totally 0s, except 1 byte that is power of 4
    return ( zerobytes == sizeof(uint)-1 && bytecheck );

}

- S O U N D W A V E October 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Or since LUT is "sparse" you can turn it into a quick function too.

bool LUT[1U << 8];  //look up: is a byte sized integer a power of 4?

//initialize: LUT[1U] = LUT[1U<<2] = LUT[1U<<4] = LUT[1U<<6] = true;

bool ispower4 ( uint x )
{
    union {
        uint whole;
        unsigned char part[sizeof(uint)];
    } var;

    bool bytecheck = false;   //whether we've seen a powerof4 byte
    int zerobytes = 0;   //count number of all-0 bytes in integer
    int i;
    
    var.whole=x;
    for(i=0; i < sizeof(uint); i++)
    {
        if( LUT[var.part[i]] ) bytecheck=true;        //i-th byte is a power of 4
        else if (var.part[i] == 0x0 ) zerobytes++;  //i-th byte is all 0s
    }
           //all bytes are totally 0s, except 1 byte that is power of 4
    return ( zerobytes == sizeof(uint)-1 && bytecheck );
}

or use this instead of LUT (calling it LUT(x) for convenience ) if you don't mind the extra time:

bool LUT(unsigned char b)   //returns true if byte "b" is a power of 4
	return( b== 1U ||b == 1U<<2 || b==1U<<4 || b==1U<<6 );

- S O U N D W A V E October 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

If they wanted THETA(lglgn) then we have to know what n is.
We can guess that n is the number (or max int).

Then lgn is ~ b where b is number of bites in max int.

Then THETA(lg(b)) suggests binary search in the "bit" space.

- S O U N D W A V E October 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

We list the following sequence:

a_0 = 4
a_1 = 4^{a_1}
a_2=4^{a_2}
...
a_n=4^{a_{n-1}}

We can find the range the integer N belongs to. say a_i, and a_{i+1}.

Now, do the previous calculation, and multiply a_i each time.

We recursively do the previous steps. Until we n=a^k or n is between a^k and a^k+1.

- Anonymous December 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If it is important to do it in exactly O(lglgn):
Let's present x = y ^ k;
We can search this power k using binary search and use binary exponentiation for checking

- Askhat December 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Have you tried N = 8, or any other multiple of 4 that is not 4^x?

- liu425 October 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yeah, I added an example.

- joe kidd October 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 votes

it fails for n = 12. it's not a power of 4

- Miguel Oliveira October 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yeah you are right. Minus the solution please.

- joe kidd October 21, 2013 | Flag


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