This page has the solutions to all interview questions posted in the blog.The questions are demystified here. The fact is that all these questions have very simple and elegant solutions. All that is required is the spark.
Links for questions:
Part 1 Solutions:
For each number, calculate the number of times it will be changed, which is nothing but the number of factors for a number. The concept is that, every square number has odd number of factors, every other number has even number of factors. So, 10 cups will be open, which are the ten square numbers, 1, 4, 9, …. 100.
There are 64 squares. Suppose we start from a black square, as mentioned. Consider it as “Position 1″. The next move will be to a white square ( that is how a horse moves ), which we shall consider as “Position 2″. The third move will be to a black one ( Position 3 ). The pattern emerges will be such that all black squares will be in odd positions and the white squares in even positions. The diagonally opposite corner is a black square, and that has to be position 64 if it must be the destination. But black squares are always in odd positions, So, it is impossible to reach the diagonal corner after moving through all the squares once.
Part 2 Solutions:
The prisoners decide that the shortest prisoner will find out the no. of black hats by looking at the prisoners in front of him (n-1). Now if the no. of black caps is even, he says “black” otherwise he says “white”. Consequently, the prisoner in the front will hear him. Now assume there were an odd no. of black hats with respect to the shortest prisoner. Then the prisoner in front of the shortest prisoner hears “white”. Now if he is wearing a black hat, he’ll see an even no. of black hats but according to the shortest one there should be odd no. of blacks. So this prisoner answers “black”. Everyone else now knows that there are even no. of black caps among the n-2 prisoners and so the process continues, at the end of which n-1 prisoners are surely saved. The only prisoner who doesn’t get saved is the shortest prisoner whose life is dependent on what we call luck and mathematicians call “PROBABILITY”
Now lets bring some complexity into this.
What if there were a crazy warden who happens to use hats of m different colours for this activity? How does the prisoners’ algo change and how many will be saved? (Assume m < n)
Hint: More the uncertainity, more the no. of unsure survivors
Assume you are one of the prisoners. You might guess the colour of your hat to be black. Now, the second prisoner will be seeing one black and one white hat. Lets assume that the second prisoner guesses the colour of his own hat to be black. Now since he sees one black hat and guesses his own to be black, he would expect the third person to answer “white”. But the third person does not answer. So the second person would easily conclude that his hat is white. But even he does not answer for some time. Thus you conclude that your hat is white.