This is the last part of posts describing my own experience interviewing with Google. You may find the first interview here and then follow the traces until this last one.
Time had already passed (almost a month) after three successive interviews with Google and this last one was most probably the critical one. I did not prepare for this as much as the previous. The whole thing tires you up and at some point it seems better to relax and concentrate on the psychology rather than the technical stuff.
I was only asked two technical questions, and since my language of choice was Java the interviewer wanted to see some Java code for the answers. In summary, this interview was perfect. While in all previous interviews, mostly the first and the second, I had some important flaws in my performance, but this one was different. And all this because I was playing in my field.
Google at some point asks for a CV. However, the way I see it, Google and other major software companies search always for something unconventional in the candidate's application. You could say of course you know 5 or 10 programming languages, or that you can build an internet spider in 1 minute, however there might be some knowledge you have, that is somewhat rare and hence you should mention. In my case, this is Number Theory.
For me, number theory is a passion that has not passed over time. It was love in the first sight, when I became aware of it, maybe at the age of 15 or so, and from that time it was the field mathematics that I was really feeling comfortable with. Anyway, it is not hard to see why it has been named as the "Queen Of Mathematics". First, it is very easy to grasp the basics (primes, divisors etc) since they are inherently tractable by the human brain. After all, every human being starts my learning how to count, which is the first step of the true intelligence, aka understanding that 5 ice creams and 5 cars, share something in common: that they are 5. This helps solving silly cases like, if I ate 5 ice creams and then I ate 1 more, how many did I eat? There is no need to think of ice creams, nor the eating process. All you have to do is to construct the proper model for this situation: use natural numbers and solve 5+1.
Another great thing with number theory, is that your 'lab' can be a blank piece of paper. You can argue that 15 is divisible by 3, but all you need is a paper to perform the division. While a physicist might say that in light speeds some hypo-atomic elements, called mambojambions, are created, he still needs a gigantic, CERN-like laboratory to test his theories.
Anyway, I myself was raised in a Pythagorean culture(numbers are holy) and so I claimed in my CV to know some number theory stuff. Although I was a little worried if I would be able to defend such claims (after I would be talking to Google), it soon proved to be a wise decision. Both in phone and on site interviews with Google, there was no better time for me than doing number theory. And to much of my surprise, and despite that Google's name is inspired by a natural number (or a unit of measure if you wish), all the 'Googlers' that I spoke and met with, did lack elementary knowledge on the field. This time, (I assume) the interviewer had dug in my profile and wanted to see for himself. Every question and conversation was related to Number Theory.
So, the first question was about sets: "Create a function, called powerSet(), that will output the power set of the input set." The power set in Algebra theory is the set of all subsets of a set (no..bull-set!) If a set has N elements then the power set will have 2^N elements. So if a set is denoted by {a,b} with a,b as elements then the power set is { {},{a},{b},{a,b} }.
The {} is the empty set. Note that all elements of the power set are sets. Recursion can help for solving this problem. If for example the powerSet function can produce power sets for sets that have at most N elements how should we solve for the N+1 case?
If we denote sets with capital letters and the initial set as A, we can use the following algorithm
1. define B set initially empty
2. a = extract one element from A
3. add powerSet(A) to B
4. for every set in B, say X, add a, and then add it to B
5. return B
Making the proper checks, this yields a function that can produce the power set of a set. For the Java case you can use the java.util.Set interface to write the code. I was asked to write the Java code but not asked any further questions, like complexity, runtime and so on. So we moved to the next and final question.
This was: "Create a function, called findZeros(), that will compute the number of zeros in the decimal representation of n!". Now this is interesting and despite seemingly easy it has subtle points. For example, 5! = 1.2.3.4.5= 120 has one zero and 10! = 1.2.3.4.5.6.7.8.9.10 = 3628800 and has two zeros. Now the obvious answer is to produce this C-style pseudo code:
int findZeros(int n) {
z<-0;
N<- n!
while ( N%10==0) { z++; N/=10;} return z;
}
It seems right but has a terrible flaw: it fails at very 'small' values of n, in our 4-byte world, for n=15. Using some Java code like below, we can verify this case.
int n=1,i=2,m=1;
while(n>=m) {
m=n; n*=i;
System.out.println("Previous value "+m+" Current value "+n+" Counter "+i);
i++; }
System.out.println("OOPS! Overflow!");
So, if our code works only for 14 cases, it is pretty much useless. We should do better than that.
For number theory geeks, like myself, this means show time! The equation below is called the Legendre's formula
and basically computes the factorization of n!. The exponent in which prime p is 'participating' in the factorization is the sum in the figure. Now, we should use it for our case. 10 is the product 2x5. So, the exponents of 2 and 5 in the factorization of n! defines how many zeros we get. For example, 10! = 1.2.3.4.5.6.7.8.9.10 = (2^8).(3^3).(5^2).7 So 2 is found 8 times and 5 is found 2 times in the factorization, hence if we pair up the 2's and 5's we get two zeros. Now, there are obviously more 2's than 5's in the expansion so finally we have to answer at the question:
What is the exponent of 5 in the factorization of n!?
Based on the reasoning above this is equal to the number of zeros of n!.
Based on Legendre's formula we have to calculate:
[n/5]+[n/5^2]+[n/5^3]+... to infinite. This is done for example by the following Java code
calculateZeros(int n) {
int s=0,r,p=5;
while((r=(n/p)) !=0)
{s+=r;
p*=p;}
System.out.println(n+"! has "+s+" zero"+((s==1)?"":"s"));
}
For example, 25 = 1.2.3....23.24.25 = 15511210043330985984000000. Running the above code will produce the message 25! has 6 zeros. Instead of messing with a number with 26 digits (25!) you only have to deal with the number 25. Why?
In the sum S(n) = [n/5]+[n/5^2]+[n/5^3]... the first term, [n/5], computes how many numbers up to n are multiples of 5. For the 25 case there are 5 of them: 5,10,15,20,25. The second computes all multiples of 25, i.e. only one. The third and all others are zero. A number that in its factorization, the prime 5 is in the power of k, e.g. 10 = 2.5 and k=1 or 25 = 5^2 and k=2, participates in the first k terms of the sum by adding one to each term. Hence its overall contribution is k, hence the sum S(n) calculates the power of 5 in the factorization of n!, and therefore the number of zeros in the decimal representation, Q.E.D.
That was it! There is no other way to handle such big values and the solution can deal with really gigantic numbers without explicit calculation. The interviewer had elementary number theoretic knowledge but was able to follow the reasoning and we had a very nice conversation after that. He seemed to be fond of such mathematical tricks as the one we dealt with.
In summary, I think this was the interview question that booked me the plane ticket to fly to the Google site. I was very satisfied after we hang up and I was impatient for the outcome but deep inside I was sure it was an ideal interview session and that my chances were great. The next morning I received a phone call from Google inviting me for the on site interviews. Mission was accomplished.
This post ends a series of posts in which I wrote about my phone interviews with Google along with many interview questions. For the on site interviews I am bound to an NDA so maybe I will post them encrypted! That's all for now about Google interview questions. Until, I come back for the on site experience remember a small tribute to Number Theory: "Die ganzen Zahlen hat der liebe Gott gemacht, alles andere ist Menschenwerk" (Numbers were created by good Lord. Everything else is human's work-Leopold Kronecker)
25 comments:
Wow! Google seems like the ultimate destination for every programmer's career.
All the best
Another funny and clever story. Your solutions were original, cool and smart!
Very nice interview..
I Read your whole interview series with Google. Very interesting! Did you finally get through? Would love to know it
damn no..
:)
That's a pity! Google is missing out on someone really good. I wish you all the very best!
u r very kind thanx! My best wishes too!
Very intelligent answer, dude!
I would like to know your bro
:)
use a mirror!
By the way calculateZeros(27) gives 6 instead of 9.
indeed, along with 4 others. But is there a 27 in the text?
I thought they wanted to have generic solution for any N.
the 'calculateZeros' works for any n.
Do you mean an analytic solution?
I just tried to get amount of zeros in 27!. Using Windows Calculator I see that 27! = 10888869450418352160768000000, which gives us 9 zeros, where the function returns only 6.
Or did I get original task incorrectly ?
Actually 27! ends with 6 zeros.
So many digits messed up your eyes!
:)
Got it :) Then original definition of the task misses word "trailing".
ruslan, you are a machine! :)
I should rephrase it better:
Number of 0's refers to multiplicity of 10. good catch
{}
{a}
{b} {ab}
{c}{ac}{bc} {abc}
{d}{ad}{bd}{abd}{cd}{acd}{bcd} {abcd}
Add new character to all previously occured set.
public List< string > PowerSet(string Set)
{
List< string > result=new List< string >();
result.Add("");
foreach (char ch in Set)
{
int length=result.Count;
for (int i = 0; i < length; i++)
{
result.Add(result[i] + ch.ToString());
}
}
return result;
}
public int ExponentCount(int Num, int p)
{
return Num > 0 ? (Num / p) + ExponentCount((Num / p), p) : 0;
}
NumOfZeroes = ExponentCount(N,5);
Had it been in my case quetions seems to be tough for me but being in that particular field you can do the job.They might have want to know how really qualified you are as per your CV thats why they asked such a tough questions looking at your CV but getting interview with the goggle is not a small thing its a very dignified matter.Good to you.
-------------------------
Cv interview questions
First of all. Thanks very much for your useful post.
I just came across your blog and wanted to drop you a note telling you how impressed I was with the information you have posted here.
Please let me introduce you some info related to this post and I hope that it is useful for community.
Source: Google interview questions
Thanks again
Ngo
well, your answer demonstrates very deep knowledge - my respect.
But I think, that good algorithm for such calculation can be made without special knowledge in theory of numbers.
here is it (C language)
int calcFactorialTrailingZeros(int n) {
static int arr[9] = { 1000000000,
100000000,
10000000,
1000000,
100000,
10000,
1000,
100,
10};
int s = 1;
int zeros = 0;
int reset = 0;
int i,j;
for( i=1; i<=n; i++ ) {
s *= i;
for( j=0; j<9; j++ ) {
if( s%arr[j]==0 )
zeros++;
}
s = s%10;
if( s==0 )
s = 1;
}
return zeros;
}
It's the best time to make a few plans for the future and it is time to be happy. I've learn this put
up and if I may I desire to recommend you some interesting
issues or advice. Perhaps you could write next articles referring to this article.
I desire to learn more things approximately it!
my weblog :: diets that work fast for women
Its 3^4.. thanks for the explanation!
Post a Comment