Programmer's Interesting interview puzzles

Source: Internet
Author: User
Tags bool constant relative first row
8 bottle of wine a bottle of poisonous, employing test. 8 hours after each test result, and you only have 8 hours of time. Ask for at least (B) people to test.

A, 2 B, 3 C, 4 D, 6


Resolution: Use 3-bit 2 to represent 8 bottles of wine, as shown in the table below

Bottle serial number binary poisoning case

The first bottle of 000 was not poisoned.

The second bottle of 001 was poisoned by the first person.

The third bottle, 010, was poisoned by a second person.

Bottle IV 011 First person, third person poisoning at the same time

Bottle Five, 100, only a third person was poisoned.

Bottle Six 101 first person, third person poisoning at the same time

Seventh Bottle 110 The first two, the third person poisoning at the same time

Eighth bottle of 1113 people poisoning at the same time

Among them, the first person drank the lowest bit for 1 corresponding wine, the second person drank the middle bit for 1 corresponding wine, the third person drank the highest bit for 1 corresponding wine

Above from: http://blog.csdn.net/xc889078/article/details/8926452


Occasionally in the online to see a few originally did not see the interview puzzles, there are several topics in the domestic spread quite wide, what n personal how to divide the most fair, the house of the three bulbs by which switch control, three aircraft around the world, with matches and two ropes to measure 45 minutes of such topics, Mars has been able to archaeology, I won't say it here anymore.

1, consider a two-person game. The game is on a round table. There are enough coins for each player. They need to take turns placing coins on the table, each time it is necessary and can only place a coin, requiring the coin to be placed entirely on the table (not part of the outside of the desk), and cannot overlap with the original coins. Whoever has no place to put the new coin will lose. The forerunner of the game or the person who has a winning strategy. What this strategy is.
Answer: The forerunner places a coin in the center of the table, and the future coin is always placed in the position relative to the place where the following person had just put it. In this way, as long as the person can put, the forerunner must have a place to put. Pioneers win.

2. Use linear time and constant additional space to reverse the word (not the character) of an article.
Answer: Reverse all the characters of the entire article (the characters that are relative to each other from both ends), and then reverse the characters inside each word in the same way. In this way, the word order of the whole article is reversed, but the word itself is turned back.

3. Use linear time and constant additional space to loop a string of length n to the left by moving the M-bit (for example, "ABCDEFG" moves 3 bits into "DEFGABC").
Answer: Cut the string to grow into two halves of M and n-m. Reverse each of these two parts, and then reverse the entire string.

4, a rectangular cake, the inside of the cake has a rectangular cavity. Just one knife, how to cut the cake into equal size two pieces.
Answer: Notice that the lines that divide the rectangular area are centered through the rectangle. A line is drawn between the center of the large rectangle and the hollow rectangle, which clearly divides the two rectangles into half, and their difference is of course equal.

5, a rectangle of chocolate, initially composed of N x m small pieces. Each time you can break a piece of chocolate into two small rectangles. It takes at least a few times to break them into a small n x M 1x1 chocolate.
Answer: N x M-1 times obviously enough. This number is also necessary because the current number of blocks of chocolate can only be increased by one after each break, and dividing the chocolate into n x m blocks of course requires at least n x M-1 times.

6. How to quickly find out how many "1" are in the binary representation of a 32-bit integer. Linear time with the number of "1".
Answer 1 (about the number of digits linear): for (n=0; b; b >>= 1) if (b & 1) n++;
Answer 2 (the number of "1" is linear): for (n=0; b; n++) b &= b-1;

7, an array of size n, all numbers are positive integers that do not exceed N-1. Use O (N) time to find the number of repetitions (assuming only one). An array of size n, all of which are positive integers that do not exceed n+1. Use O (N) time to find the number that is not present (assuming only one).
Answer: Computes the sum of all the numbers in the array, calculates the sum of all the numbers from 1 to N-1, and the difference between the two is the number that repeats. Computes the sum of all the numbers in the array, calculates the sum of all the numbers from 1 to n+1, and the difference between the two is the missing number.

8, give a line of C language expression, determine whether the given integer is a power of 2.
Answer: (b & (b-1)) = = 0

9. How many points on the earth make it a mile south from that point, a mile east, and a mile north, just back to the starting point.
Answer: "North Pole" is a traditional answer, in fact, there are other answers to this question. In fact, there are infinitely many points to meet the requirements. All the miles from the Antarctic point 1 + 1/(2π) are satisfying, and a mile south will take you to the South Pole 1/(2π), a mile east, just a week after the latitude circle, and then north to the original road back to the starting point. In fact, this is still not the whole point of satisfying the requirements. From Antarctica point 1 + 1/(2kπ) is possible, where k can be any positive integer.

10, A, b two people on two islands respectively. B is sick, and a has the medicine that B needs. C has a boat and a lockable box. C is willing to carry things between A and B, but things can only be placed in boxes. As long as the box is not locked, C will steal everything in the box, no matter what is in the box. If A and B each have a lock and can only open their own lock of the key, a should be how to put things safely to B.
Answer: A put the medicine in the box, lock the box with your own locks. b After getting the box, add a lock on the box. After the box is shipped back to a, a takes off its own lock. When the box is transported to B, B takes off his lock and gets the medicine.

11. A couple invited N-1 to the party (so there was a total of 2N people at the party). Everyone shook hands with all the people they didn't know. Then, the host asked the rest of the people (a total of 2n-1 individuals) each held a few hands, the answer is all different. If everyone knew their spouse, the hostess shook hands several times.
Answer: The number of handshakes can only be from 0 to 2n-2 this 2n-1 number. Apart from the male master, there are altogether 2n-1 individuals, so each number happens once. One of them (0) did not shake hands, and a man (2n-2) and all the other couples shook hands. These two people must be a couple, otherwise the latter will shake hands with the former (thus the former handshake number is no longer 0). Apart from the couple, one person (1) shook hands with (2n-2) only one person (2n-3) and a couple other than (0). These two people must be a couple, otherwise the latter will shake hands with the former (thus the former handshake number is no longer 1). And so on, until the person who has held N-2 times and the person who has held the N-time hand is paired. At this point, all but the male owner and his spouse are paired with each other. According to the rule of exclusion, the last remaining person to shake hands is the hostess of the N-1.

12, two robots, initially in different positions on the axis. Enter the same program for both robots, making sure that the two robots can meet. The program can only contain "Shift left N units", "Move N units right", conditional judgment statement if, loop statement while, and two functions that return a Boolean value "at their own starting point" and "at the beginning of each other". You cannot use other variables and counters.
Answer: Two robots begin to move right at unit speed until a robot goes to the beginning of another robot. The robot then chases each other at double speed. The procedure is as follows.

while (!at_other_robots_start) {
Move_right 1
}
while (true) {
Move_right 2
}

13. If you are asked to choose one of the two games below, which one you choose. Why.
A. Write down a sentence. If that's true, you'll get $10, and if that's false, you'll get less than $10 or more than $10 (but not exactly $10).
B. Write down a sentence. You'll get more than $10 regardless of the true or false of the sentence.
Answer: Select the first game and write down "I will neither get $10 nor get $10000000".


14, you are in a 100-storey building, there are 21 wire thread head marked with the number 1. 21st. These wires have been extended to the top of the building and the head of the roof is marked with a letter A. U You don't know the correspondence between the numbers below and the letters above. You have a battery, a light bulb, and many very short wires. How to determine the corresponding relationship between wire head and head only once down the stairs.
Answer: In the following 2, 3 together, the 4 to 6 are connected together, 7 to 10 are connected together, and so on, so you put the wire into 6 "equivalence class", the size of 1, 2, 3, 4, 5, 6. Then to the roof, to determine which line and all other wires are not connected, which lines and the other connected, which lines and the other two connected, and so on, thus determining the letter A. Which equivalence class you belong to. Now, the first letter in each equivalence class is joined together to form a new equivalence class of size 6, and the second letter in the next 5 equivalence classes is joined together to form a new equivalence class of size 5; Go back downstairs and differentiate the new equivalence class. In this way, you can solve the problem by knowing the first few letters of the original equivalence class that each number corresponds to.

    15, a certain prescription is very strict, you need to take both A and B pills each day, you can not more or less. This medicine is very expensive, you do not want to have any little waste. One day, you open the vial of pill a, pour out a pill in your hand, then open another vial, but accidentally poured out two pills. Now, you have a pill a, two pills B in your hand, and you can't tell which one is a and which is B. How can you strictly follow the prescription pill and not have any waste.
     Answer: Cut the three pieces of medicine into two halves and put them in two piles. Take another pill A, cut it in half, and add a half piece of a to each pile. Now, each pile of pills contains exactly two pieces of a and two and a half pieces of B. Take one of these piles a day.

     16, you are on a spaceship, the computer on the ship has n processors. Suddenly, the spacecraft was attacked by an alien laser weapon, and some of the processors were damaged. You know that more than half of the processors are still good. You can ask a processor whether the other processor is good or bad. A good processor always tells the truth, a bad processor always tells lies. Use N-2 to find a good processor.
        Answer: Label the processor from 1 to N. Using the symbolic a->b to indicate to the processor labeled a that processor B is not good. First Ask 1->2, if 1 said no, they both get rid of (remove a good and a bad, then the rest of the processor is still half good), and then from the 3->4 began to ask. If 1 says 2 is good, continue to ask 2->3,3->4, ... Until one time J said J+1 is bad, remove J and j+1, then ask J-1-j+2, or start with j+2-j+3, if there is no j-1 in front (it has been removed before). Notice that you always maintain such a "chain", and each of the preceding processors says that the latter is good. All the processors in this chain are either good or bad. When the chain grows longer and fewer processors are left, there is always a time when the chain is more than half the rest of the processor, so be sure all the processors in the chain are good. Or, more and more processors are removed, the length of the chain is still 0, and finally only one or two processors have not been asked, then they must be good. Also note that the first processor is good or bad has never been asked, think about you will find that the last processor is not good or bad can not be asked (if the chain longer than the remaining half of the processor, or the last one is left out of this is only this, you do not ask), so the number of queries will not exceed n-2.

17, a disc is coated with black and white two colors, both of which occupy a semicircle. The disc rotates at an unknown speed, in an unknown direction. You have a special camera that allows you to instantly observe the color of a point on a circle. How many cameras do you need to determine the direction the disc rotates.
Answer: You can put two of the camera on the disk close to the two points, and then observe which point first color. In fact, just needing a camera is enough. The control camera moves clockwise around the center of the disc, observing how often the color changes, and then letting the camera move counterclockwise around the center of the disc at the same speed, observing the frequency of discoloration again. It can be concluded that the frequency of discoloration is slower, the camera rotation direction is the same as the disc.

18, there are 25 horses, the speed is different, but the speed of each horse is fixed value. There are now only 5 tracks that cannot be timed, that is, the relative speed of 5 horses can be known at most for each tournament. Ask at least a few times to find the top 3 of the 25 horses. ( Baidu 2008 interview questions)

Each horse has at least one chance to compete, so 25 horses are divided into 5 groups, and the first 5 games are unavoidable. It's also easy to find the champions, and the champions of each group play together in one game (6th game). The last is to find 2nd and 3rd place. We named the Group A, B, C, D, E in the first 5 games, in turn, according to the position we got in the 6th game. That is: the champion of Group A is 1th in the 6th game, and the winner of Group B is the 2nd of 6th field ... The 5 horses in each group are numbered from fast to slow according to the results they have been racing:

Group A: 1,2,3, 4,5
Group B:3,4,5
Group C:1,2,3,4,5
Group D: 1,2,3,4,5
Group E: 1,2,3,4,5

From the information we have now, we can know which horses have been excluded from the 3. As long as 3 or 3 horses are already known to be faster than this horse, it has been eliminated. As you can see, only the 5 horses in the bold blue in the table above are likely to be 2 or 3. namely: 2, 3 in Group A, 1, 2 in Group B, 1th in Group C. Take the 5 horses for the 7th game, the first two of the 7th game is 25 horses in 2, 3. So there are at least 7 games.

There are some variants of the problem, such as 64 horses looking for the top 4. method is the same, in the 1th place after the search for the next 3 candidates will be able to.

19, IBM written questions: A census clerk asked a woman, "How many children do you have, how old are they?" ”
The woman replied: "I have three children, their age is multiplied by 36, the old is equal to the house number next door." The census clerk immediately went to the side room and looked at it and said, "How much information do I need?" The woman replied, "I am very busy now, my oldest child is sleeping upstairs." "The census clerk said:" Thank you, I know. ”
Question: What is the age of the three children?
1x2x2x3x3 =
All the possibilities are
1,1,36;sum = 38
1,2,18;sum = 21
1,3,12;sum = 16
1,4,9;sum = 14
1,6,6;sum = 13
2,2,9;sum = 13
2,3,6;sum = 11
3,3,4;sum = 10
Since the census-takers know the age and are still unsure of the age of each child, the likelihood is
1,6,6;sum = 13
2,2,9;sum = 13
Since the largest (implying only one of the largest) children are sleeping, it is only possible that
2,2,9;sum = 13

20, there are 7 grams, 2 grams weight each one, the balance of one, how to use these items three times will 140 grams of salt into 50, 90 grams each.
A: The first step: divide 140 grams of salt into two equal parts, each 70 grams.
Step two: Put the balance on the side of the 2+7 gram weight, the other side of the salt, so that 9 grams and 61 grams of separate salt.
Step Three: Place 9 grams of salt and 2 grams of weight on one side of the balance and salt on the other, so that you get 11 grams and 50 grams. So 50 and 90 were separated.

21, there are three baskets of fruit, a basket is full of apples, the second basket is full of oranges, the third basket is orange and apples mixed together. The label on the basket is deceptive, (for example, if the label is an orange, you can be sure that there is no orange in the basket, maybe there is an apple) your task is to take out one of the baskets, take only one fruit from the inside, and then correctly write out the label of the three baskets of fruit.
A: From the basket with apple and orange tags to take out a fruit, if it is an apple, that the basket is full of apples, then the apple-labeled basket is full of oranges, the orange-labeled basket with apples and oranges; If a fruit is an orange, it means that the basket is full of oranges, So the orange-labeled basket was full of apples, apples and oranges in the basket with Apple tags.

22. The topics are as follows:
0 1 2 3 4 5 6 7 8 9
_ _ _ _ _ _ _ _ _ _

Fill in the numbers on the line to meet the requirements.
Requirements are as follows: the number of the corresponding number is filled in, representing the number of the above appears below the number of times, such as 3 below is 1, representing 3 to appear below.

the correct answer is: 0 1 2 3 4 5 6 7 8 9
6 2 1 0 0 0 1 0 0 0

My idea is: because the number of the second line is the number of the first row appears below the number of times, the following 10 lattice, a total of 10 times ... So the sum of the 2nd row numbers is 10.

First start from 0, first fill 9, certainly impossible, 9 below if 1, only 8 bits filled 0, not enough to fill the 8,8 below to fill at least 2, the back no longer think, because there are 7 positions left, not enough to fill 0 ... So the analogy. I'll get the answer above 0 when I fill 6.

In fact, two key conditions can be introduced for this topic:
1. The sum of the 2nd row numbers is 10.
2, two rows of numbers multiplied by the sum is also 10.
The answer to these two conditions is to write the program implementation below.

Original value: 0,1,2,3,4,5,6,7,8,9//Number of occurrences: 6,2,1,0,0,0,1,0,0,0 #include "iostream" using namespace std;
    #define Len class NUMBERTB {private:int Top[len];
    int Bottom[len];
BOOL success;
    PUBLIC:NUMBERTB ();
    int *getbottom ();
    void Setnextbottom ();
int getfrequecy (int num);
};
    NUMBERTB::NUMBERTB () {success = false;
    Format top for (int i = 0; i < len; i++) {top[i] = i;
    }} int *numbertb::getbottom () {int i = 0;
        while (!success) {i++;
    Setnextbottom ();
} return bottom;
    }//set next bottom void Numbertb::setnextbottom () {bool ReB = true;
        for (int i = 0; i < len; i++) {int frequecy = Getfrequecy (i);
            if (bottom[i]! = frequecy) {Bottom[i] = Frequecy;
        ReB = false;
}} success = ReB;
    }//get frequency in bottom int numbertb::getfrequecy (int num)//Here num refers to the upper row of the number i {int count = 0;
for (int i = 0; i < len; i++) {        if (bottom[i] = = num) count++; } return count;
    cout that corresponds to frequecy} int main (void) {int i;
    NUMBERTB NTB;
	int *result = Ntb.getbottom ();
	cout<< "Original value:";
	for (i=0;i<10;i++) cout<<i<< "";
	cout<<endl;
    cout<< "Number of occurrences:";
    for (i = 0; i < len; i++) {cout << *result++ << "";
	} cout<<endl;
    System ("pause");
return 0;
 }


  Reprint Please indicate the source, original address: http://blog.csdn.net/hackbuteer1/article/details/6726419

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.