One on a big internet company pen test sharing

Source: Internet
Author: User

Statement:

First of all, I didn't take part in the exam (my old man). I'm just hearsay. Does the test question really exist? Who knows!

At this point, but want to share some knowledge.

The original question about the text description:

Given a two-dimensional array, fill in 0 and 1 randomly. For the upper and lower left and right continuous (diagonal not connected) the number of 1 around 0.

Here you can think of the 1 data as an island, the length of the island coastline, that is, the number of 0 around.

Introduction:

Seen the game of human machine-eating chess Game (ii) Chi bo Friends, should be a moment to have ideas. In fact, the chi of Go, in the absence of the eye position, is calculated to calculate the solid island coastline. In the case of an eye position. The go calculation also calculates the coastline length and the outer coastline length inside the Hollow islands. What is the eye position of Weiqi, please Baidu. We are not going to talk about the eye position of Weiqi.


Solution Ideas:

I do not know the specific question, so it is not known whether special treatment is needed for the length of the inner coastline of the hollow Island. It's just that. It is not difficult to detect the presence of an eye position when calculating gas, and labeling is possible. The assumption is indeed to include Hollow islands. That is to form the eye position of Weiqi, 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 train of thought, not necessarily good enough, but can solve this problem.

1. Determine the minimum bounding box for the internal coastline (it is possible to have an external coastline, but to exclude most of the outer coastline), which can save a very large number of operations.

2. Check if the coastline within the bounding box can reach the shortest boundary of the surrounding Manhattan distance, assuming it can be reached. And the points on the coastline connected to it are reachable, both of which are external coastlines.

3. Otherwise, assume that you cannot walk to this boundary of the bounding box. It is proved that it is surrounded. The internal coastline, and the points connected to it, belong to the internal coastline.

4. According to the above method, we can find out the internal coastline by detecting all the non-repeated points inside the bounding box.

We know that this algorithm is inefficient (with minimal bounding box.) And the nature of the connecting point. Actually, the number of paths to be searched should be very small). But it is right that the inner point of the encircled coastline, like a little monster trapped in an island, can never escape.

The point on the outer coastline will only feel like an obstacle to the island.

This algorithm can handle arbitrarily complex graphics, even if the mountain Road 18 bends, external points will also go through the trouble to reach the coastline, Hollow Island has a lot of hollow. There is no mistake in the interior point or external point.


For the 2nd use of depth-first search or astar algorithm can be, we aim to find a way. Rather than finding the shortest way.


Summary:

when I sent the blog, I did not abstract the QI algorithm into a 1, 0 questions.

Daring to push the test, 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 question, but ignore the article that has substance rather than the pen question. I hope you can extract abstract knowledge from various blogs. Status Quo

Suppose you have a better idea, to be able to comment under the discussion.



Another example, a long time ago I participated in a company written test, encountered a problem such as the following, given an array, the values in the array are random and disorderly. It is required to put even numbers in front of the array, and odd rows behind the array. Also hope your algorithm time complexity is as low as possible. Be able to answer in C + + language.

What do you think of the problem? Suppose you have an excellent method in a moment. I have to congratulate you. Because years ago I did think for a long time to write the correct but slightly higher time complexity than the standard code. Later read some information, said the standard answer using the first and last two pointers to solve the problem. Well, today I would like to say that the standard answer should be updated. Or the subject should have many other limitations. Look at the code and you're clear.

1#include <iostream>2#include <algorithm>3 using namespacestd;4 intMain ()5 {6vector<int> nums{1,2,3,4,5,6,7,8,9,Ten};7 Random_shuffle (Nums.begin (), Nums.end ());8Partition (Nums.begin (), Nums.end (), [] (intEle)BOOL{returnEle%2==0;});9      for(auto& ele:nums) {cout<<ele<<" ";};Tencout<<Endl; One}

Only 10 lines of code. Contain everything.

Only the 8th line of code, the problem is complete.

Haha, I do not want to see this blog post. Because perhaps he would have no moral to add a sentence after the title prohibit the use of STL.

And suppose you are going to participate in the written test of the little comrade, remember to bring the STL siege weapon, let the city's people bright blind eyes.

Be assured that there is nothing to optimize the code for the problem. Very difficult (but it is still possible.) Some very professional questions. However, this is generally not a problem and now faces the problem in the performance of STL. First, the horizontal question is how people compare with STL writers. Second, the C + + optimizer compiler knows more about STL than your code.


One on a big internet company pen test sharing

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.