Baidu side, Summary of failure experiences

Source: Internet
Author: User

In other words, because I knew a brother of Baidu, I took the internal push process and did not take the test. At about three o'clock P.M., I printed my resume and ran a copy of my resume from the school gate to Baidu's Putian building. Arrive 4 o'clock in advance and wait for a while at the front-end. I took me to the room and made a place in the middle office. Now, the interview starts.

The first question: how to find the place to enter the ring in a single-chain table with a ring. At that time, I almost fainted when I heard the ring, and said, "I'm done. This is the end of the game. Under the guidance of the elder brother, I thought about it on the left and thought about it. I didn't finish it. The elder brother finally got depressed and said, Let's ask something else first.

The second problem is how to allow a private host to communicate with a public host. This is easy. After all, I will give him SNAT and DNAT to solve the problem, and he said yes.

The third problem: He compiled it by himself, saying that one private network A, several hosts AI, And the other private network B are connected by several hosts bi, bi is also interconnected. BI only has one host bn (n! = 1) You can connect to the Internet. In AI, there is only one physical connection between A1 and B1. Ask how to connect the host in A to the Internet. After hearing this, I was dizzy and asked a white question. Do these hosts have routing functions? No. Okay. I can use a tunnel. He said, it is a way to solve this problem.

At last, my brother started to introduce me to the work content of their departments and asked me what I was interested in. I know that my project experience is not in match with their departments, I want to switch to another department, and finally I will wait for two notifications one week later.

My side failed.

In general, my algorithms are too weak. Although I have read the introduction to algorithms during the summer vacation, I just forgot about them. Next I will post the solution to the first problem to sacrifice the spirit of my side.

 

 

 

 

1. How to determine whether a linked list is such a linked list?
2. If the linked list shows an existing ring, what if the entry point of the ring is found?

Answer:

1. Determine whether the linked list has a ring. The method is as follows:

Set two pointers (fast and slow). The initial values all point to the header. Slow moves one step forward each time, and fast moves two steps forward each time. If the linked list has a ring, fast must first enter the ring, when slow enters the ring, the two pointers must meet each other. (Of course, if the first line to the end of fast is null, It is a loop-free linked list) The program is as follows:

Bool isexitsloop (slist * head)
{
Slist * slow = head, * fast = head;

While (Fast & fast-> next)
{
Slow = slow-> next;
Fast = fast-> next;
If (slow = fast) break;
}

Return! (Fast = NULL | fast-> next = NULL );
}

2. Find the entry point of the ring

When fast encounters slow, slow certainly does not traverse the linked list. For more information, see note (1). Fast already loops through N circles (1 <= N) in the ring ). Assume that slow takes the s step, then fast takes 2 s step (the number of fast steps is equal to the number of S plus N turns on the ring), set the ring length to R, then:

2 S = S + nR
S = nR

Set the length of the entire linked list to L. The distance between the entrance ring and the encounter point is X, and the distance from the start point to the entrance point is.
A + x = nR
A + x = (n-1) R + r = (n-1) R + L-
A = (n-1) R + (L-a-x)

(L-a-x) is the distance from the encounter point to the ring entry point. From this point, we can see that from the chain table header to the ring entry point is equal to (n-1) the cycle inner ring + the encounter point to the ring entry point, so we set a pointer from the head of the linked list and from the encounter point. Each time we take a step, the two pointers must meet each other and the first point of the encounter is the ring entry point. The program is described as follows:

Slist * findloopport (slist * head)
{
Slist * slow = head, * fast = head;

While (Fast & fast-> next)
{
Slow = slow-> next;
Fast = fast-> next;
If (slow = fast) break;
}

If (fast = NULL | fast-> next = NULL)
Return NULL;

Slow = head;
While (slow! = Fast)
{
Slow = slow-> next;
Fast = fast-> next;
}

Return slow;
}

Expansion problems:

Determines whether two single-chain tables are intersecting. If yes, the first vertex of the intersection is given (both linked lists do not have loops ).

There are two better methods:

1. Connect one of the linked lists to the beginning and end, and check whether the other linked list has a ring. If so, the two linked lists are intersecting, and the detected dependency ring entry is the first vertex of the intersection.

2. If the two linked lists intersect, the two linked lists are the same nodes from the intersection to the end of the linked list, we can traverse one linked list first until the end, and then traverse another linked list, if you can also go to the same end point, the two linked lists will intersection.

Now let's write down the length of the two linked lists and traverse them again. The long chain table node starts to step forward (lengthmax-lengthmin), and then the two linked lists move forward simultaneously, each step, the first point of an encounter is the first point of the intersection of two linked lists.

Note 1

If R is set to the ring length, the fast pointer R/2 can complete a circle. if R is an odd number

The system traverses all vertices in the loop. we will definitely meet slow in R. if R is an even number, the nearest distance between fast and slow can be 2 or 1 in R/2 (F catches up with S ). if the value is 2

Fast + 2 = slow

Therefore, in the next step, the slow position is slow + 1, while fast is fast + 2;

Next step slow + 2, fast + 4;

Because fast + 2 = slow

Slow-2 + 4 = slow + 2

Because when the closest distance is 1, the two pointers must take one step to meet each other.

So the two pointers meet each other. The maximum number of steps is S <= r/2 + 2 (r> = 4)

 

 

 

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.