Baidu interview question objective answer [original], original interview question answer

Source: Internet
Author: User

Baidu interview question objective answer [original], original interview question answer

Recently, I have been looking for a job. I often send interviews with some big companies in the class group to test the questions. I received such a question yesterday, which is said to be Baidu's interview question.

There is a 27 cm fine wood pole, each of which has an ant in the five positions 3 cm, 7 cm, 11 cm, 17 cm, 23 cm. The wooden pole is very small and cannot pass through an ant at the same time. At the beginning, the head of the ant financial system is either left or right. They only move forward or turn their heads, but do not move back. When any two ants meet, they both turn their heads to the opposite direction. It is assumed that the ant Financial can walk 1 cm away every second. Write a program to find the minimum and maximum time for all ants to leave the wooden pole.

After reading this question, I was suddenly very interested. Today I made it for half a day, and it took about one and a half hours. the question of a large company is really an expert. I will not talk much about it because it has been implemented using algorithms. Let's take a look at the code. I tried to write all the comments in the code. there are two classes in total. One is the Ant model and the other is the control class. the original code can be obtained here:

Http://www.blogjava.net/Files/itspy/baidu.rar



//////////////////////////////////////
/* Baidu interview questions
* There is a 27 cm-inch fine wood pole, each of which has an ant in the five positions 3 cm, 7 cm, 11 cm, 17 cm, and 23 cm.
* The wooden pole is very fine and cannot pass through an ant at the same time. At the beginning, the head of the ant financial system is either left-facing or right-facing. They only move forward or turn their heads,
* But it does not return. When any two ants meet, they both turn their heads to the opposite direction. It is assumed that the ant Financial can walk 1 cm away every second.
* Write a program to find the minimum time and maximum time for all ants to leave the wooden pole.
*
*
* Analysis: the ant in the question can only meet at the integer point, and cannot meet at other points, such as 5cm, that is, each ant can take 1 second, and then
* Check whether an encounter exists.
*
* In this way, the idea of my program implementation is to initialize five ants, let each ant take one second, and then check whether there is an encounter. If so, do the corresponding processing. When each ant
* When I exit the wooden pole, I will record the current time. In this way, we can see how long it will take to exit the wooden pole in the current state and traverse all the States to get
* Possible.
*/

Package baidu;

Public class Ant {
/*
* Step indicates the length of each ant time unit.
*/
Private final static int step = 1;

/*
* Position indicates the initial position of the ant Financial.
*/
Private int position;

/*
* Direction indicates the forward direction of the ant financial. If it is set to 1, it indicates the direction of 27 cm. If it is set to-1, it indicates the direction of 0.
*/
Private int direction = 1;

/*
* If this function is run once, it indicates that the ant has been moving forward for one unit of time. If it has been removed from the pole, an exception is thrown.
*/
Public void walk (){
If (isOut ()){
Throw new RuntimeException ("the ant is out ");
}
Position = position + this. direction * step;
};


/**
* Check whether the ant financial has walked out of the wooden pole. If it does, true is returned.
*
*/

Public boolean isOut (){
Return position <= 0 | position> = 27;
}

/**
* Check whether the ant has encountered another ant
* @ Param ant
* @ Return if true is returned
*/
Public boolean isEncounter (Ant ant ){
Return ant. position = this. position;
}

/**
* Changing the forward direction of ant financial
*/
Public void changeDistation (){
Direction =-1 * direction;
}


/**
* Constructor: sets the initial forward direction and initial position of the ant Financial.
* @ Param position
* @ Param direction
*/
Public Ant (int position, int direction ){
This. position = position;
If (direction! = 1 ){
This. direction =-1; // set the initial position in the direction. For example, if it is 0, it is also set to 1. this facilitates subsequent processing.
} Else {
This. direction = 1;
}
}

}

 

//////////////////////////////////////// /////////////////


Package baidu;

Public class Controller {

Public static void main (String [] args ){

Int time = 0;
For (int I = 0; I <32; I ++ ){
Ant [] antArray = getAntList (getPoistions (), getDirections (I ));
While (! IsAllOut (antArray )){
For (Ant ant: antArray ){
If (! Ant. isOut ()){
Ant. walk ();
}
}
Time ++;
// Check whether any Ant has met. If yes, change its forward direction.
DealEncounter (antArray );
}
System. out. println (time );

// Set the time to 0. In this way, you can reset the condition and obtain the time required for completing the whole process again.
Time = 0;
}

}

/**
* The algorithm of this function is messy, but it can solve the problem temporarily.
*
* @ Param list
*/
Public static void dealEncounter (Ant [] antArray ){

Int num_ant = antArray. length;
For (int j = 0; j <num_ant; j ++ ){
For (int k = j + 1; k <num_ant; k ++ ){
If (antArray [j]. isEncounter (antArray [k]) {
AntArray [j]. changeDistation ();
AntArray [k]. changeDistation ();
}
}
}

}

/**
* Because there are 5 Ant combinations, there are 32 combinations after the combination. it is represented by five binary digits. If it is 0, it indicates that Ant is going in the direction of 0. If it is 1, it indicates that Ant is going in the direction of 27.
*
* Note: when the initial value is set through Ant constructor, 0 is changed to-1 by filtering.
*/
Public static int [] getDirections ctions (int seed ){
Int result [] = new int [5];
Result [0] = seed % 2;
Result [1] = seed/2% 2;
Result [2] = seed/4% 2;
Result [2] = seed/8% 2;
Result [4] = seed/16% 2;

System. out. println ("directions is" + result [0] + "|" + result [1] + "|"
+ Result [2] + "|" + result [3] + "|" + result [4]);

Return result;

}

/**
* Batch set the initial positions of Ant. This setting is not necessary and can be set directly in the code.
*
* @ Return
*/
Public static int [] getPoistions (){
Return new int [] {3, 7, 11, 17, 23 };
}

/**
* Get the 5 Ant instances with the set initial values
*
* @ Param positions
* @ Param directions ctions
* @ Return
*/
Public static Ant [] getAntList (int [] positions, int [] directions ctions ){
Ant ant3 = new Ant (positions [0], directions [0]);
Ant ant7 = new Ant (positions [1], directions [1]);
Ant ant11 = new Ant (positions [2], directions [2]);
Ant ant17 = new Ant (positions [3], directions [3]);
Ant ant23 = new Ant (positions [4], directions [4]);

Return new Ant [] {ant3, ant7, ant11, ant17, ant23 };
}

/**
* Determine whether all the Ant products are out of the wooden pole, that is, set the exit condition.
*
* @ Param antArray
* @ Return
*/
Public static boolean isAllOut (Ant [] antArray ){
For (Ant ant: antArray ){
If (ant. isOut () = false ){
Return false;
}
}
Return true;
}
}


Common Interview Questions and answers

The most common interview questions are self-introduction, motivation for job search, understanding of the company, strengths and weaknesses. As for the answer, there is no standard!

Answer to interview questions

In reality, your starting point must focus on the job responsibilities of your interview. In fact, in the next five years, everyone will have many plans and many things to do, but the interviewer asks you, of course you should start from the job you are interviewing now.

Since you interviewed a sales manager assistant, you can reply to him: "If I am lucky enough to become a member of the company, I will be in the first year, fully learn and master the job skills as a sales manager assistant, and assist the sales manager in completing sales within the specified time. At the same time, I will try to figure out how to become a competent sales manager and hope to become a sales manager in the second and third years, I believe that in the next five-year plan, I will be able to go further. Napoleon once said: "A soldier who does not want to be a general is never a good soldier !" I think this sentence is especially suitable for the sales industry. I also believe that the company will not bury a good sales talent!

The above is the answer for the job of the sales manager assistant you interviewed. Similarly, when you interview other jobs. In the case of similar problems, we should follow this rule to first start from your position, and then let the interviewer think that you are a positive, hard-working, willing and learning person, in future plans, you have the desire to realize your self-worth and gain the company's approval. Note: You should use the right words. Do not describe how you want to get promoted. This will give the other party a feeling that you are not willing to be down-to-earth. If you interview your direct supervisor, it will also make the other party misunderstand that you want to occupy the nest, as long as the other Party feels that you want to be recognized, it is a good wish to be promoted in the next five years, of course, according to the company's culture, the job you are engaged in varies in nature and complexity, and the Promotion frequency will be different. However, you will continue to strive for this opportunity with a hard-working attitude. If you find that the other party is not satisfied with your expression, add it. Pay attention to what you say and wish you a successful interview!

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.