Google Interview Questions and Answers
December 1, 2010
I came across a very interesting article this morning regarding common Google Interview questions. While I don’t plan on working for Google (not saying I wouldn’t like to – I just don’t think I’ve got the kind of experience they’re after) I do enjoy challenging interview questions and I thought I would see how I fare with them.
I tried to do the questions without resorting to Google (no pun intended), so if my answers seem idiotic keep that in mind. You’ll notice that I only looked at the Software Engineer questions.
So without further ado…
The questions
Why are manhole covers round?
I’ve heard this one before. In fact, this is such a common interview question that I’m pretty sure this never gets asked. Manhole covers are round for 2 reasons – so that the cover can’t actually fall into the manhole and so that they’re easier to move (roll them).
What is the difference between a mutex and a semaphore? Which one would you use to protect access to an increment operation?
Both mutexes and semaphores are used to control access to a shared resource – most often in multithreading scenarios. A mutex is used when only one thread or process is allowed to access a resource and a semaphore is used when only a certain set limit of threads or processes can access the shared resource. Essentially a mutex is a semaphore where the limit is set to 1.
Which one would I use to protect access to an increment operation? Well, if I’m using C# I would say neither – just use Interlocked.Increment – but in the general scenario I would use a mutex.
A man pushed his car to a hotel and lost his fortune. What happened?
He lost an epic game of Monopoly. (Heard this one before I think)
Explain the significance of “dead beef”.
I think there might be some cultural difference that makes this question more difficult to me than it should be – I’ve never heard of “dead beef”. If I had to venture a guess I would say that it’s similar to a sunk cost – something that is done and cannot be changed.
Write a C program which measures the speed of a context switch on a UNIX/Linux system.
Finally, some code! I’ve never attempted something like this before, so I’m fairly confident I’m going to fail miserably – but that’s half the challenge in my opinion. I’m not going to use C (I haven’t done C programming in almost 5 years) – I’ll stick with C#.
I guess there are 2 challenges to measuring a context switch – performing a context switch and measuring the time it took. I guess the easiest way to perform a context switch would be to use a lock and simply have 2 threads constantly loop over the lock. The easiest way to measure it would be to use the Stopwatch class. The challenge here would be to try and minimize the overhead with the lock and the actual measurement with the Stopwatch. As I said – I think I’ve got this completely wrong but I’ll give it a go.
static void Main(string[] args)
{
new Thread(ThreadCall).Start();
ThreadCall();
Console.WriteLine("Ticks per second: {0}", Stopwatch.Frequency);
Console.WriteLine("Average ticks per context switch: {0}", times.Average());
Console.ReadLine();
}
private static readonly object @lock = new object();
private static IList<long> times = new List<long>();
private static Stopwatch stopwatch = null;
private static void ThreadCall()
{
while (true)
{
lock (@lock)
{
if (stopwatch == null)
{
if (times.Count > 1000000)
{
break;
}
stopwatch = Stopwatch.StartNew();
}
else
{
times.Add(stopwatch.ElapsedTicks);
stopwatch = null;
}
}
}
}
Well, it measures something which is a good start. We actually avoid the overhead of instantiating the stopwatch since the actual measurement will only run between the instantiation of the stopwatch and where we add the measurement to the list. There is the obvious overhead of the null check.
Running this in release mode gave the following output:
Like I said – it measures something, but I don’t think it’s the correct answer. If this is correct my cpu can do about 460 000 context switches per second.
Given a function which produces a random integer in the range 1 to 5, write a function which produces a random integer in the range 1 to 7.
I think this is probably my favourite question so far. The question doesn’t really state how random the result should be, but I guess the more random it is the better. I’m going to try the implementation in C# again, although if I’m feeling up for it I might do a Ruby implementation later on.
public class Randomizer
{
private Random random = new Random();
public int GivenFunction()
{
return random.Next(1, 6);
}
public int MagicFunction()
{
var bigRandom = GivenFunction() * 100000
+ GivenFunction() * 10000
+ GivenFunction() * 1000
+ GivenFunction() * 100
+ GivenFunction() * 10
+ GivenFunction();
return (bigRandom % 7) + 1;
}
}
That’s easy enough – but how random will the results be. To test it, I ran both the ‘given function’ and my ‘magic function’ a million times and counted the results. So I pretty much counted the number of 1’s, 2’s, 3’s all the way to 5 for the given function and 7 for the other function. These are the results:
Which is actually very.. random! I’m quite impressed with the results given my rather naive implementation. This could obviously be optimized.
This is about all I can handle for now, I might try some more of the questions in a future post. Happy coding.
Tags: C#
Re: dead beef, it's a hex code of 4 bytes, typically used as an example IP address. It also happens to be a magic number used in the JRE as I recall, because of the fame of the term. It falls into the same category as "cafe babe" in that regards
Re: the random function, the trick is not to keep the randomness, but to keep the uniformity of the randomness (definition of random is "each value generated is independant of the last one", thus "return 4; // generated by rolling a dice" is not really random), the uniformity ensures that 1 will happen as often as 2, 3, 4, 5, etc., given a large set of generated random numbers.
I'm not going to be able to test the uniformity of your MagicFunction(), but you might want to give it a try at some point
Thanks for the feedback!
I've never heard of dead beef or cafe babe, thanks for the help.
I agree with you on keeping the uniformity – the test I ran is only one test for randomness – there is obviously loads more. I actually wrote a test application for a random number generator at University – maybe I should go fiddle around with that code.
I believe, DEAD BEEF in this context is related to memory initialization in C. It comes from "Writing Secure Code" by Michel Howard and David LeBlanc. They suggest to set freed memory to DEAD BEEF pattern, so that you can see that you can easily detect access to already released memory. That's true – it will stand out in your debugger (they also suggest to go step-by-step through your code at least once).
From the Wikipedia entry on magic numbers:
DEADBEEF Famously used on IBM systems such as the RS/6000, also used in the original Mac OS operating systems, OPENSTEP Enterprise, and the Commodore Amiga. On Sun Microsystems' Solaris, marks freed kernel memory (KMEM_FREE_PATTERN)
The significance of deadbeef is 0xdeadbeef, which is a 32-bit integer … used in al sorts of locations.
http://en.wikipedia.org/wiki/Hexspeak for a partial list
Inspiring post! Good work Jaco!
2 Questions though:
1. why use thread to do the timing? One thread seems to be perfect for time recording
2. Dont think the algorithm you gave can generate a true random (0-6) distribution. As the last digit can only be 0-4, you are giving 1 and 7 more chance to show up than the others.
Your test results revealed that too.
If I was to answer quickly on most of these questions I would for sure never get a job at Google, and I dont know many who would.
But I like the idea of testing people like this, instead of just the usual talk about your resume.