Two Tencent interview questions

Source: Internet
Author: User
Tags blank page

First of all, let's take a look at the mental power of preschool children ~ Sorry!

After reading it, I ran into tears. This question is not impossible for any literary and artistic children ......

Okay, go to the theme. Today's theme is: no tooth decay! Hey, joke!

First, let's look at the first question: there are 1000 Identical bottles, of which 999 are normal water and one is poison. Any creature that drinks the poison will die one week later. Now, you only have 10 mice and a week. How can you determine which bottle contains toxic drugs?

Generally, for the sake of openness, the number of white mice is not fixed. Instead, how many white mice are needed? (Of course, the fewer the better the mouse, the better the life ). This is an intellectual question that involves basic computer science. This mainly refers to the examinee's divergent thinking ability, so it does not require the interviewer to be able to do it correctly in the first time.

The first thing you can think of is that you need to drink one bottle at a time, and then wait for the result, the water in the corresponding bottle will be poisonous and will not die, that is the only bottle of water that is not fed to the mouse is toxic, but this method uses too many mice. However, it is not totally no good. It is the solution with the lowest average number of dead mice in the experiment.

Let's try again, and quickly approach the results using the bipartite method. In the first step, we will convert 1000 bottles of water into A and B, and then mix 500 bottles of water into A and B, give a white mouse a drink. If the white mouse does not die, divide heap B into two heap C and D (if the white mouse dies, divide heap a into two heap C and D ), mix the 250 bottles of water in the C heap to a white mouse and wait for the result ...... And so on, you know, it's always like this. To determine the final result, we need 10 mice. However, there is a question in this way, that is, the waiting time is too long to meet the question requirements. In addition, if you are not lucky, 10 mice may all die, and the mice are really miserable.

It seems that we need to spread it again. According to the question, we can only feed the mouse once (more than once ). First, we can reduce the scale of this problem. If it is two bottles of water, then one mouse is enough. What if it is four bottles of water? Suppose there are four bottles of water: A, B, C, D, and D. We can mix a + B and feed it to the first day of a mouse, and A + C to the second day of a mouse, if both the 1st and 2nd ends, A is poisonous. If neither is dead, D is poisonous. If only 1st ends, B is poisonous, if only 2 is dead, C is toxic. Well, it's a bit interesting. After a mouse is fed with water, the final result is two states of birth or death. That is to say, two mice can represent up to four States (according to the principle of arrangement and combination ). How many mice are needed to indicate the 1000 states? 10, because the 10th power of 2 is 1024> 1000, the question is how to combine these bottles to feed the mouse.

First, the 1000 bottles of water are numbered from 0 to 999, and then 10 bits are used to represent these bottles. 10 mice are numbered from 1 to 10.

0 0 0 0 0 0 0 0 0 water bottle

0 0 0 0 0 0 0 0 1 water bottle

0 0 0 0 0 0 0 1 0 2 water bottle

0 0 0 0 0 0 0 1 1 3 water bottle

0 0 0 0 0 0 1 0 0 4 water bottle

0 0 0 0 0 0 1 0 1 5 water bottle

0 0 0 0 0 0 1 1 0 6 water bottle

......

1 1 1 1 1 0 0 1 1 1 999 water bottle

 

1 2 3 4 5 6 7 8 9 10 Mice

Then, we set the rats 1-10 to the top 10 binary digits. The rule for each mouse to feed water is: if the corresponding binary digit of a bottle is 1, feed it to the mouse, if the corresponding binary system is 0, it will not be fed. For example, the mouse on the 10 th will drink the water bottle on the 1, the water bottle on the 3, the water bottle on the 5 ,...... Mixed Water with water bottle 999. After the feed is complete, it will wait for the result.

The dead mouse marked 1 and the dead mouse marked 0. If only the rats on the 10th die, 0 0 0 0 0 0 0 0 0 1. The result is that the water bottle on the 1st is toxic. If the mouse died on the 8, 9, 0 0 0 0 0 0 0 1 1 0, the result is that the 6 water bottle is toxic. If they are not dead, the 0 water bottle is toxic, apparently, the water in the water bottle No. 0 in the above mixed method was not fed to any mouse. Why? In fact, it is easy to understand that all the mice fed with toxic water are dead, and the rest are not dead. It is so simple. Generally, there was no such big data during the interview. Generally, there were 4 mice and 16 bottles of water (it was really the test of thinking process ).

In fact, this method is supported by a Theoretical Algorithm: the bloom filter algorithm. If you are interested, you can check the information.

The mouse is really miserable.

 

The second question is a basic C ++ question. I often take this question to test the type of resume on which the interviewer is proficient in C ++ (of course, it is generally a freshman ). First, prepare a blank page and write the following code on Area:

Class
{
Public:
Void function () {printf ("Hello World ");}

};

......

A * P1 = NULL;
P1-> function ();

......

 

Write the same piece of code on its B side, with only a little difference:

Class
{
Public:
VirtualVoid function () {printf ("Hello World ");}
};

......

A * P1 = NULL;
P1-> function ();

......

Then, let's start to ask, first give the interviewer the program with the program, and then ask what the program will do. The possible answer is: one is the program's direct crash, and the other is the program's output Hello world, the program outputs Hello world, crash, and flip the paper to ask the interviewer what the program will do. If the interviewer can answer this question correctly and explain it clearly (the key is to explain it clearly), it indicates that the subject's c ++ basics are good.

The real answer to this question will not be published. It is very easy to know the reason. You can check the assembly code of the following Program (at a Glance ):

      #include <stdio.h>      class A      {      public:            void function(){printf("Hello World");}      };      class B      {      public:            virtual void function(){printf("Hello World");}      };      int _tmain(int argc, _TCHAR* argv[])      {            A* p1 = NULL;            p1->function();            B* p2 = NULL;            p2->function();      };

A * P1 = NULL;
004113fc mov dword ptr [P1], 0
P1-> function ();
00411403 mov ECx, dword ptr [P1]
00411406 call A: function (4110e6h)

B * P2 = NULL;
0041140b mov dword ptr [P2], 0
P2-> function ();
00411412 mov eax, dword ptr [P2]
00411415 mov edX, dword ptr [eax]
00411417 mov ESI, ESP
00411419 mov ECx, dword ptr [P2]
0041141c mov eax, dword ptr [edX]
0041141e call eax
00411420 cmp esi, ESP


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.