Public comments written examination

Source: Internet
Author: User

This afternoon I attended the public comments examination. First, I had a variety of questions similar to the civil service examination, such as language reasoning, digital reasoning, and graphic reasoning. I found that I was really weak in this regard, there is no time to do it in the future, and there are various strange questions about graphic reasoning... Finally, I gave two algorithm questions for half an hour. The questions are as follows:

 Question 1: In a directed acyclic graph with a single entrance and a single exit, you must insert some nodes in some places so that the number of nodes that any one experiences from the start point to the end point is the same, similar to the following figure, describe the algorithm and analyze the time complexity.

As shown in, there are two paths between node A and node C. The path ABC passes through A node, while the path AC passes through 0 nodes, all we need to do with our algorithm is to add a node in the center of the AC path, and then both the ABC path and the ADC path pass through a node.

The solution I provided on the exam:

(1) first, obtain the length n of the longest path (that is, the longest path goes through n-1 nodes). This can be done using DFS and should be easier.

(2) traverse each node pointing to the exit node from the exit node. If the node is already the Start Node, insert n-1 nodes between the two nodes, otherwise, the process is called recursively, but the path length passed to the node is reduced by 1.

Is this solution correct? Consider the right image (Ignore the red part of the graph.) According to my algorithm, the obtained n should be 3, and then recursion to node 4, it is found that node 1 is already the Start Node, then insert a new node 8 between node 4 and node 1. Then the problem occurs: The longest path has changed to 4! So this algorithm is wrong. Then I thought about it again. Can the recursive process start from the Start Node?

Start? The node to be added is inserted between the last two nodes in the entire path. For the figure just now, this seems to solve the problem, but we add red nodes to the figure just now, when we use this algorithm, we first get the longest Path 4, and then press the length of the 1478 path to 3. According to the algorithm, we will add a node between node 7 and node 8, the problem just occurred again. Any path that passes through node 78 with the original path length of 4 is now changed to 5, and the problem just occurred, so this algorithm is also incorrect. To sum up, we can find that the reason for the two algorithms being incorrect is that the node location is incorrect, if we add a node in the correct position (in the relative picture on the right, we add a node in node 4 and node 7, and add two nodes between node 4 and node 8 ), how can we achieve this? I think of an algorithm like this: maintain a set of information for each node, including the number of layers of the node (the path length from the Start Node to the node, and the Start Node is set to 0) and the parent node that generates the length. Compared with the figure on the right, the unprocessed information maintained by node 6 is: Layer 2 comes from node 3 and node 4. The unprocessed information maintained by node 7 is: layer 3 comes from node 5, node 6, and Layer 2 from node 4. The unprocessed information of node 8 is: Layer 4 comes from node 7, and Layer 3 comes from node 7, layer 2 comes from node 4. All we need to do in our algorithm is to change the layer information to be maintained for each node, that is, no matter from the path to the node, the layers of the node are fixed. The algorithm is as follows:

1. initialize the layers of the Start Node.

2. Traverse each path from the Start Node and generate a maintenance information for each node.

(1) If the node does not have maintenance information, create it;

(2) If the node has maintenance information, there are two situations:

(A) if the number of layers of the generated maintenance information is the same as the number of layers of the original maintenance information, merge the two maintenance information. For example, for the graph in the example, the original maintenance information of node 5 is "Layer 3 comes from node 2", and the maintenance information generated from node 3 to node 5 is "Layer 3 comes from node 3". Because the layers are the same, we can merge it into "Layer 3 comes from node 2 and node 3 ";

(B) if the number of layers of maintenance information generated is different from the number of layers of maintenance information on the original node, we need to compare the number of layers of the original node to a large one:

A. if the number of layers of maintenance information is large, you only need to insert a new node between the node that generates the maintenance information and generate maintenance information for the new node, then start the process from the new node (2)

B. if the number of layers of new maintenance information is large, the new node information will be saved to this node. Then we need to insert a new node between all the nodes that generate the original maintenance information and this node, and the process starts from all newly inserted nodes (2 ).

 

The whole algorithm is like this. We can store the path (or node) to be traversed using stacks ~

Of course, this is only a solution that I have come up with for the time being. There may be errors in it, and there must be more efficient algorithms. Thank you for your comments and suggestions ~

 

  Question 2: When the forum administrator manages the Forum, he needs to find the channel owner in the Forum. The definition of the channel definition is as follows: the total number of posts in a certain period of time is N, if a post with more than N/2 is sent by an ID, the ID is filled with water. Design an algorithm to find this ID in the shortest time.

In fact, this is the issue of searching for the crowd. The first thing we can think of is to extract the IDs of all the posts and then sort them to get the most intermediate ID of the sorting result, because if there is a watering er, the number of his IDS is greater than N/2, of course, there may also be mistaken kill. After we get this ID, we will traverse all the IDs again, obtain the number of IDs. The time complexity of this method is the time complexity of various sorting algorithms.

Given the number of N/2, we can have a simpler algorithm: In all IDs, we retrieve two different IDs without returning them until we can continue to get them, that is to say, the remaining IDs are the same. If the sprinkler exists, this ID must be the ID of the sprinkler. Of course, there is still a possibility of mistaken killing. We can continue to determine it through traversal; this method can be used to promote the number of posts to N/A. We only need to get A different ID each time. When A is greater than 2, some details may exist, which are relatively simple.

According to this idea, when we set it to N/2, we have a linear time algorithm. The pseudo code of this algorithm is as follows. begin and end point to the start position and end position of the array storing ID respectively:

 1 string find_most_appear(string *begin, string *end){ 2     string str = *begin, appear_times = 1; 3     ++begin; 4     while(begin != end) 5     { 6         if(*begin == str) 7         { 8             ++appear_times; 9         }else if(appear_times != 0){10             --appear_times;11            }else{12             str = *begin;13             appear_times = 1;14             }15         ++begin;16     }

Of course, it is still possible that there may not be water-filling persons, so verification is still required. time complexity O (n) is nothing to say.

It is still laborious to do two steps in half an hour. It is time to think of this question ~

This article is from the big margin, and we are brave enough to clear all obstacles!

Thank you for your reading and your comments.

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.