A big on the internet company's pen test to share

Source: Internet
Author: User
Statement:

First of all, I did not take part in this exam (I am old), I just hearsay, whether there really exists this question. Who knows. Just want to share some knowledge with you. The original question probably describes:

Given a two-dimensional array, inside the random fill 0 and 1, to get the upper and lower left and right continuous (diagonal not connected) of the number of 1 around 0. Here you can think of 1 as an island, the length of the coastline of the island, that is, the number of the surrounding 0. Intro:

Seen the game of human machine-eating chess Game (ii) Chi Bo Friends, should be a moment to have ideas. Actually, in the case of no eye position, the calculation of a solid island coastline is counted. In the case of an eye position, the go calculation gas is also the length of the shoreline and the outer coastline of the Hollow Island. What is the eye position of go, please Baidu, we do not continue to discuss the eye position of Weiqi.


Solution Ideas:

I do not know the detailed question, so it is not known whether special treatment is needed for the length of the inner coastline of the hollow Island. However, it is not difficult to detect the presence of an eye position when calculating gas, and labeling is possible. If it is really a hollow island, that is to form the eye position of go, we can carry out special treatment. We just have to deduct the total numbered the internal eye position numbered is OK, how to deduct is a problem.

Here, I say a thought, not necessarily good enough, but can solve the problem.

1. Determine the minimum bounding box for the internal coastline (it is possible to have an external coastline, but most of the outer coastline can be ruled out), which can save a lot of operations.

2. Detect if the shoreline within the bounding box can reach the shortest border of the Manhattan of the bounding box, if reachable, and the points on the coastline connected to it can be reached, both of which are external coastlines.

3. Otherwise, if it is not possible to walk to this boundary of the bounding box, it is proved that it is surrounded by the internal coastline and that the points connected to it belong to the internal coastline.

4. The above method detects all the non-repeating points inside the bounding box, which is the internal coastline.

We know that this algorithm is not efficient (with the smallest bounding box, and the connection point nature is the same, in fact, the number of paths should be very few), but it is right, surrounded by the shoreline inside the point, like trapped in the island of the small monsters, can never escape out. And the points on the outer coastline just think the island is an obstacle.

This algorithm can handle any complex graphics, even if the mountain Road 18 bends, the external point will also go through the trouble to reach the coastline, Hollow Island has a lot of hollow, there will not be mistaken internal points or external points.

For the 2nd use depth first search or Astar algorithm can be, we aim to find the road on the line, rather than find the shortest way.


Summary:

When I sent the blog, I did not abstract the QI algorithm into a 1, 0 questions. Make a bold guess, perhaps the question is to see the relevant blog post, only to sprout out the idea of this problem. I think there should be some people in the CSDN pay more attention to the content of the questions related to the question, and ignore the content of this substance rather than the pen test article, I hope you can extract the abstract knowledge from various blogs, status quo.
If you have a better idea, you can leave a message under discussion.



To add an example, a long time ago I took part in a company written test, encountered a question as follows, given an array, the values in the array are random and disorderly, require the even row in front of the array, odd rows in the array behind, but also hope that your algorithm time complexity as low as possible. You can use the C + + language to answer.

How do you think this to the problem, if you have an excellent way, I have to congratulate you, because a few years ago I did think for a long time to write the correct but the time complexity than the standard answer slightly higher code, and then read some information, said the standard answer using the first two pointers to solve the problem. Well, today I would like to say that the standard answer should be updated, or the topic should be more restrictive. Read the code and you'll understand.

1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 int main ()
 5 {
 6     vector<int> nums{1,2,3,4,5,6,7,8,9,10};
 7     Random_shuffle (Nums.begin (), Nums.end ());
 8     partition (Nums.begin (), Nums.end (), [] (int ele)->bool{return ele%2==0;});
 9 for     (auto& ele:nums) {cout<<ele<< "";};
Ten     cout<<endl;
11}

Just 10 lines of code, including everything. Just the 8th line of code, complete the problem. Haha, I do not want to see this blog post. Because maybe he will. Add a sentence behind the topic prohibit the use of STL.

And if you are about to take part in the written test of the little comrade, remember to bring the STL siege weapon, let the city's people bright blind eyes.

There is a point to be assured that the question of how to optimize the code, it is also difficult (but still possible, some very professional problems, but this problem is generally not an interview problem bar) in the performance of more than STL. First, the level of the question of how can be compared with STL writers. The second is that the C + + compiler knows better about optimizing STL than your code.

Related Article

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.