[Interview in person]
1. Given a list of numbers, describe an algorithm to find the maximum number on that list.
Sol: Compare the first two and record the maximum in a variable called MAX. Move on to the third one, compare it to MAX, and update MAX with the maximum between these two. Keep doing that as you run over the array of numbers. At the end MAX will have the maximum number of the list.
2. How about for finding the second largest number on that list?
Sol: Of course you could run twice over the array, but there's a better way to do it. Compare the first two and record the maximum in a variable called MAX1 and the other one in a variable called MAX2. Move on to the third one and compare it to MAX1. If it is larger than MAX1, then update MAX2 with the current value at MAX1, and MAX1 with the third number on the array. If it is not larger, then update MAX2 with the maximum between the current value at MAX2 and the third number. Continue doing this over the entire array and at the end MAX1 and MAX 2 will have the maximum and the second maximum of the list.
3. There are eight coins that look exactly the same, but one of them is heavier than the other seven. The other seven all weight the same. With a scale and two tries, how do you find the heaviest coin?
Sol: Divide the group into three groups of 3, 3 and 2. Weigh the two groups of 3. If they weigh the same, then the heaviest coin is in the group of 2 and one more use of the scale will give us the heaviest coin. If they don't, then you are left with one group of 3 containing the heaviest coin and one more use of the scale with two of those three coins will give you the heaviest coin.
4. There are five containers with pills. One of the containers has only contaminated pills and the other four have only non-contaminated pills. With one scale and one try, how would you find the container with the contaminated pills if a contaminated pills doesn't weigh the same as a non-contaminated pill?
Sol: I wasn't asked this question but heard people talking about it afterwards. I started thinking like if they were the coins of question 3., but that might take two tries. After a while I had no idea. I looked it up when I got home. I found it here (question #10) with way too many other riddles. I don't really like riddles (that I don't know know how to solve ;)).
1. Given a list of numbers, describe an algorithm to find the maximum number on that list.
Sol: Compare the first two and record the maximum in a variable called MAX. Move on to the third one, compare it to MAX, and update MAX with the maximum between these two. Keep doing that as you run over the array of numbers. At the end MAX will have the maximum number of the list.
2. How about for finding the second largest number on that list?
Sol: Of course you could run twice over the array, but there's a better way to do it. Compare the first two and record the maximum in a variable called MAX1 and the other one in a variable called MAX2. Move on to the third one and compare it to MAX1. If it is larger than MAX1, then update MAX2 with the current value at MAX1, and MAX1 with the third number on the array. If it is not larger, then update MAX2 with the maximum between the current value at MAX2 and the third number. Continue doing this over the entire array and at the end MAX1 and MAX 2 will have the maximum and the second maximum of the list.
3. There are eight coins that look exactly the same, but one of them is heavier than the other seven. The other seven all weight the same. With a scale and two tries, how do you find the heaviest coin?
Sol: Divide the group into three groups of 3, 3 and 2. Weigh the two groups of 3. If they weigh the same, then the heaviest coin is in the group of 2 and one more use of the scale will give us the heaviest coin. If they don't, then you are left with one group of 3 containing the heaviest coin and one more use of the scale with two of those three coins will give you the heaviest coin.
4. There are five containers with pills. One of the containers has only contaminated pills and the other four have only non-contaminated pills. With one scale and one try, how would you find the container with the contaminated pills if a contaminated pills doesn't weigh the same as a non-contaminated pill?
Sol: I wasn't asked this question but heard people talking about it afterwards. I started thinking like if they were the coins of question 3., but that might take two tries. After a while I had no idea. I looked it up when I got home. I found it here (question #10) with way too many other riddles. I don't really like riddles (that I don't know know how to solve ;)).