A few days ago interviewed a company, gave two algorithm topics. Thought it was very interesting, when the interview answer when the answer is not very good. Then came back to find the information. Record it.
First, the topic one
There are 1000 bottles of water, one of which is poisonous, and the mice will die 24 hours after tasting a little poisonous water, at least how many mice can identify the bottle of water in 24 hours. Landlord this topic at that time did not answer a very good plan. Only think of this topic should be linked to the binary. Came back and looked up the information on the Internet, since there is such an ingenious idea.
Label 1000 bottles with the following labels (10-bit lengths):
0000000001 (1th bottle)
0000000010 (2nd bottle)
0000000011 (3rd bottle)
......
1111101000 (1000th bottle)
Remove 1 drops from all the bottles in the last 1 digits of the number 1 and mix them together (e.g. from the first bottle, the third bottle, ...). One drop mixed together) and marked with a mark of 1. And so on, remove 1 drops from all the bottles where the first digit is 1 and Mark 10. Now get 10 numbers of mixed liquid, White rat Platoon station, respectively labeled 10, 9, ... Number 1th, and give them the corresponding number of the mixed liquid respectively. 24 hours later, come over and do the autopsy:
From left to right, the dead mouse labeled 1, not dead paste on 0, the last to get a serial number, the number is replaced by 10 numbers, is the poisonous bottle of water number.
Check: If the first bottle is poisonous, according to 0000000001 (1th bottle), the mixture of the 1th is poisonous, so the white rat's life and death character is 0000000001 (the white mouse numbered 1 is hung), 12 into the decimal = 1th bottle is poisonous; If the third bottle is poisonous, , 0000000011 (bottle 3rd), the 1th and 2nd mixtures are poisonous, so the white rat's life and death character is 00000011 (the mouse brother numbered is hung), and the 112 binary label is converted to decimal = 3rd bottle is poisonous.
Second, topic Two
There are n individuals, one of the stars and n-1 a crowd, the masses all know the stars, the stars do not know any masses, the understanding between the masses and the masses do not know, now if you are a robot r2t2, every time you ask a person whether you know another person's price for 0 (1), try to design an algorithm to find the star, And give the time complexity. The landlord then answered the double cycle of what kind of situation. But the time complexity of that situation is O (n*n).
Solution one: After seeing the topic, the first thought is to choose one of the people as the target object, and then ask the remaining n-1 individuals. If the current target does not know other people, but others know TA, the goal is the star, otherwise, the target is not a star, in the selection of other people as the goal to do the same inquiry. The time complexity is O (n*n).
Solution Two: In the solution one, each time only asked to do not record the relevant information, such as when asked A and B, there are several relationships:
1) A does not know B, B does not know A. ==> A and B are not stars.
2) A know B, b know a. ==> A and B are not stars.
3) A recognize B, B do not know A. ==> A is not a star, B may be a star.
4) A does not know B, B knows a. ==> A may be a star, B must not be a star
Because you can exclude at least one object per query, you only need to n-1 the query to find the target.
Summarized as follows: Is A know B? (yes) A is not a star, B may be a star: (No) A may be a star, B is a crowd. So a single question can be sure of a person
1 intFunc38 (intA[],intN2{3 intI4 intStari = 0;//Store minimum number of positions in a, star position5 intFlag = 0;6 7 for(I=1; i<n; i++)8{9 if(A[stari] > A[i])//If the number of Stari labels is less than the number of I labels, indicating A[stari] a[i]Ten{ OneStari=i; Aflag=0;//Replace suspect (minimum number), Mark Clear 0 -} - Else if(A[stari] ==a[i])//If the Stari is really the only minimum number, it won't come in here. the{ -flag++; -} -} + if(flag>0) - return-1;//means no stars, for example all the numbers are equal + A returnStari at}
See:
http://lzj0470.iteye.com/blog/657579
Http://www.cnblogs.com/legendmaner/archive/2013/05/24/3097248.html
Interview algorithm Questions