Google pen question sorting (1), Google pen question sorting

IT famous enterprise interview: Google pen questions.

Google is a company that many IT people want to go to. The written examination and interview are the only way to go. Now, during the recruitment season on campus, IT is a shortcut to attack and sort out the classic pen exam of IT famous enterprises! For this reason, I sorted out some information on the network.**Very classic test questions**And the answer. Reading carefully will be helpful !!

**1. Google pen: determines whether a natural number is the square of a certain number. Of course, you cannot use open operations.**

Assume that the number to be judged is N.

Method 1:

Traverse the numbers from 1 to N, obtain the square and compare them with N.

If the square is less than N, the traversal continues. If the square is equal to N, the system exits successfully. If the square is greater than N, the system fails to exit.

The complexity is O (n ^ 0.5 ).

Method 2:

Use the binary search method to determine the numbers between 1 and N.

The complexity is O (log n ).

Method 3:

Because

(N + 1) ^ 2

= N ^ 2 + 2n + 1,

=...

= 1 + (2*1 + 1) + (2*2 + 1) +... + (2 * n + 1)

Note that these items constitute an equal difference sequence (2 difference between each item ).

So we can compare the relationship between N-1, N-1-3, N-1-3-5... and 0.

If the value is greater than 0, the system continues to subtract. If the value is equal to 0, the system exits successfully. If the value is smaller than 0, the system fails to exit.

The complexity is O (n ^ 0.5 ). However, in method 3, multiplication in method 1 is replaced by addition and subtraction, so the speed is faster.

Google pen: How to Select 1000 keywords at random

A given data stream contains endless search keywords (for example, keywords that people continuously enter during Google search ). How can we randomly select 1000 keywords from this endless stream?

Defines an array with a length of 1000.

The first 1000 keywords in the data stream must be put in the array.

For the n (n> 1000) keyword in the data stream, we know that the probability of this keyword being randomly selected is 1000/n. Therefore, we use this keyword to replace the random one in the array with a probability of 1000/n. This ensures that all keywords are selected with a probability of 1000/n.

We can make sure that 1000 random keywords are always stored in the array.

**2. Google pen: sort the following expressions by complexity**

Sort the following expressions by complexity

2 ^ n

N ^ Googol (Googol = 10 ^ 100)

N!

N ^ n

The complexity is from low to high.

N ^ Googol

2 ^ n

N!

N ^ n

**3. Google pen: Randomly select a point in the circle with a radius of 1**
Assume that the center of the circle is in the coordinate element (0, 0 ).

Method 1.

Select a random point in the square of the X axis [-1, 1] and Y axis [-1, 1. Then, determine whether the vertex is in the circle (by calculating the distance from this vertex to the center of the circle ). If it is in the circle, this point is the result. If it is not in the circle, it is re-selected until it is found.

The square area is 4, and the circle area is pi. Therefore, the probability of random points in the Square within the circle is pi/4.

Method 2.

Select an angle randomly from [0, 2 * pi), corresponding to a radius in the circle, and then select a point on the radius. However, the points on the radius cannot be evenly selected. The selected probability should be proportional to the length from the center of the circle, so that the random points are evenly distributed within the circle.

**4. Google pen: How to randomly select a number for an integer stream with an unknown length**

There is a method called reservoir sampling, which is to randomly select a number!

Method 1.

Save the entire integer stream to an array and then randomly select it.

If the integer stream is too long to be saved, this method cannot be used.

Method 2.

If the integer stream ends after the first number, we will definitely select the first number as the random number.

If the integer stream ends after the second number, the probability of selecting the second number is 1/2. We replace the preceding random number with 1/2 with a probability of 2nd to obtain a new random number that meets the conditions.

....

If the integer stream ends after the nth number, the probability of the nth number is 1/n. We replace the selected random number with the nth number with the probability of 1/n to obtain a new random number that meets the conditions.

....

In this way, we only need to save a random number and the length of the integer stream to date. Therefore, you can process any long integer stream.

**5. Google pen: design a data structure, which contains two functions: 1. Insert a number and 2. Obtain the middle number. And estimate the time complexity.**

1. Use array storage.

Insert a number to the end of the array within the O (1) time.

When getting the number, find the number in the O (n) time. (Compare the first number of arrays with other numbers and divide them into two groups based on the size of the comparison results. Then we can determine the group in which the number is located. Further segment the group according to the same method until the middle number is found .)

2. Sort array storage.

When you insert a number, locate the position to be inserted in the O (logn) time, move the element in the O (n) time, and insert the new number to the appropriate position.

When a medium number is obtained, a medium number is found in the O (1) complexity.

3. Use a large root heap and a small root heap for storage.

Use a large root heap to store half of the smaller numbers, and use a small root heap to store half of the larger numbers.

When you insert a number, insert the number to the corresponding heap within the O (logn) time, and move the root node as appropriate to keep the two heap numbers equal (OR 1 difference ).

When getting the number, find the number in the O (1) time.

Given a fixed-length array, the incremental integer sequence is written into this array. When it is written to the end of the array, the returned array starts to be rewritten and overwrites the previously written number.

Find the given integer in this special array.

Suppose the array is a [0, 1,..., N-1].

We can adopt a binary search-like strategy.

First, compare a [0] And a [N/2]. If a [0] <a [N/2], it indicates a [0, 1 ,..., n/2] is an incremental subsequence. Otherwise, the other part is an incremental subsequence.

Then, judge whether the integer to be searched is within the ascending subsequence range. If yes, use the common Binary Search Method to continue searching. If not, repeat the search process until it is found or fails.

Specify two sorted sequences to find the common elements.

Assume that the sequence is sorted in ascending order. Define two pointers pointing to the beginning of the sequence respectively.

If the two elements are equal, an identical element is found. If the two elements are not equal, the pointer pointing to the smaller element moves forward.

Repeat the preceding steps until a pointer points to the end of the sequence.

Google interview questions

A friend received an interview with Google a few weeks ago and revealed some questions during the interview. By the way, I also sorted out the content I heard from other people who have been interviewed. Google, the biggest Internet company, has a quiz set to see if you can answer it. Many of these questions are open-ended and there are many correct answers, so no answers are provided here.

How many golf balls can be mounted on a school bus?

You are reduced to only a coin with a very high thickness (not squashed, but scaled down proportionally), then thrown into an empty glass blaster, and the blade starts to rotate in one minute. What do you do?

How much will you charge for cleaning all the windows in Seattle?

How can we identify whether the computer's memory stack overflows upwards or downwards?

To explain what a database is to your 8-year-old nephew, please complete it in three sentences.

How many times does the clock pointer overlap in a day?

You need to go to B from A, but you don't know if you can. What should you do?

For example, if you have a wardrobe filled with shirts, how can you sort out the shirts to make it easier for you to find them later?

There was a town with 100 couples, and every husband was deceiving his wife. Wives cannot tell the lies of their husbands, but they can know whether any other man is lying. The law in the town stipulates that no adultery is allowed. Once the wife proves that the husband is unfaithful, he should be killed immediately. All women in the town must strictly abide by this Law. One day, the town's queen declared that at least one husband was unfaithful. How did this happen?

Every family wants to have boys in a country where boys and girls are important. If the children they give birth to are girls, they will be reborn until the children are born. What is the ratio of men to women in such a country?

If the probability of driving a car within 30 minutes on the highway is 0.95, you can see the probability of driving a car within 10 minutes (assuming that the probability is normal)

If you see the time of the minute is, what is the angle between the hour hand and the minute hand? (Certainly not 0 degree !)

4 In the evening, they will go through a tent and return to their camp. Unfortunately, they only have one flashlight that can last for another 17 minutes. A flashlight is required to pass through the primary node, and each secondary node can only hold the size of two people. The four of them have different speeds. It takes one minute for the first to walk through the shard, two minutes for the second, five minutes for the third, and 10 minutes for the slowest one. How can they all go through the shard in 17 minutes?

You and your friends attended the party. A total of 10 people, including both of you, were present. Your friend wants to bet with you that every time a person has the same birthday as you, you will give him 1 yuan. Every time a person has a different birthday, he will give you 2 yuan. Will you accept it?

How many piano tuner are there in the world?

You have 8 balls of the same size, 7 of which have the same weight and the other are heavy. How can I use the balance to retrieve the duplicate ball twice.

Five pirates are listed in the order of 5 to 1. The largest pirate has the right to propose how to share 100 gold coins. However, if most people disagree, they will be killed. What kind of solution should he propose, so that he can get as many gold coins as possible and won't be killed? (Tip: A pirate can get 98% gold coins)

Do you think you are sure you want to work on Google?

A Google test question to help

Do you also want to test GOOGLE? It is much harder for me to answer questions during an interview at Tianya. For example, I can't tell you what to do. How can I help you? What about your mailbox?