Problem description:There are 1000 barrels of wine, one of which is toxic. Once eaten, toxicity may occur after one week. Now we use the mouse for experiments. We need to find out the bucket of poisonous wine in a week and ask how many rats are needed at least.
Answer:Rats: 10, 2 ^ 10 = 1024> 1000
Solution:
(1) Wait for 10 mice in a queue: 0-9 and keep them in cages.
(2) number 1000 barrels of wine in sequence and convert them into binary values, for example, 8 = 1000. The mouse wine at the corresponding position is displayed according to the number of bits in the binary format. Because we chose 10
Mice, 2 ^ 10 = 1024> 1000, can ensure that all the numbered wines can be fed to mice without missing the 1 bits converted to binary.
(3) long wait, waiting for mouse death
(4) records dead mice in sequence, for example, 00001 00010 = 34.
Analysis:We remember the two numbers or the "|" operation, as long as one result is always 1, this is the principle, when the toxic wine is numbered binary to feed to mice, only the fed mice die, and the dead mice record the binary of the toxic liquor. The result of the record is the binary value of the toxic liquor, and the number can be obtained after conversion.
Thoughts:After seeing the results, I felt that the problem was very simple. If I had some ideas, I would be able to give the answer soon. Some gave me 2000 barrels of wine and we could give the answer quickly:
2 ^ 11 = 1024*2> 2000, that is, 11. If this is the first time you encounter something really hard to think about, it is like finding a character in a string to see if it appears. Someone thinks
The solution is to match 26 characters with a prime number, and multiply the corresponding Prime Number of the string and letter to be searched to get a result, as long as the result is divided by the prime number of the letter to be queried
Value, indicates the appearance of the character string in the letter is found (someone will ask, this scheme should at least traverse the string to be matched, and the worst of a single match
The time complexity is o (n), but if you want to perform multiple searches in this string, the prime number solution is a good solution ).
Think again:I am confused, but I remember that there was an interview question that found a string in hundreds of millions of records. Let's look at the following solution.
Solution 1: insert hundreds of millions of records into the database and directly query the strings in the database.
Disadvantage: it occupies a large amount of space and the query speed is slow.
Solution 2: add hundreds of millions of records to the data by hash, and search strings by hash in the database.
Disadvantage: over million records are also hash-based, occupying space
Advantage: Based on solution 1, the hash will reduce the space.
Solution 3: With Bloom Filter, you only need to apply for a space of 20 times that of the string to achieve string matching at a low and acceptable failure rate of 0.0000.
Assume that the number of strings is 0.1 billion 1*10 ^ 8, apply for 20*1*10 ^ 8 bit size, and select 10 hash function hashes, perform 10 for each of the 0.1 billion records
Hash, set 1 on the requested bit. Hash the string to be queried 10 times and obtain 10 results. The query value is
1. For more information, see Bloom Filter.
Disadvantage: it is difficult to select 10 hash functions.
Advantage: it saves a lot of space and achieves good big data processing in the case of low conflicts and failures.
Summary:When you see the interview questions, think about them right away and study in sequence to make yourself a good designer.
Record your learning for further study.