Common algorithm written or interview questions

Source: Internet
Author: User

Problem 1
: Is it a loop? (Judge whether the linked list has a ring ?)

Assume that wehave a head pointer to a link-list. Also assumethat we
Know the list is single-linked. Can you come up an algorithm
Checkwhether this link list contains des a loop by using O (n) time and O (1)
Space wheren is the length of the list? Furthermore, Can you do so
O (n) time and onlyone register?

Method: Use two pointers, starting from scratch, one for one forward node and one for two forward nodes, the maximum is 2n, and the last two pointers can overlap. If there is no ring, the system stops normally.

Similarly, you can find the intermediate node of the linked list. Same as above.

 

Problem 2:
Design a complexity of NAlgorithmFind the M element at the bottom of the linked list. The last element is assumed to be the reciprocal of 0th.

Tip: Double-pointer search

 

Problem 3:
Use the simplest method to determine the number of a long integer. A is 2 ^ N (the N power of 2)

Tip: X & (x-1)

 

Problem 4:
Two beaker, one sugar, one salt, one spoon of sugar to the salt, stir evenly, then a spoonful of mixture will put sugar in the beaker, ask you which two beaker contains many impurities?

Tip: Same. Assuming that the impurity is not equal, then the impurity is put into the original cup, then the weight of the content in the cup will change, unreasonable.

 

Problem 5:
Provide you with two files a and B, each storing 5 billion URLs, each occupying 64 bytes, and the memory limit is 4 GB, so that you can find the common URLs of files a and B.

Method 1: Use a hash table. Create a hash table using the element A, and control the hash to an appropriate scale. Find the Element B in the hash. The URL that cannot be found first exists in the new file. If yes, the corresponding hash table item is deleted. When the hash table item is less than a threshold value, the new elements in a are re-hashed. Loop again.

Method 2: Add a record to files A and B in the hash table. If a table item does not exist, it is placed in the hash table, and the same item is deleted. Note: There may be many repeated items, causing frequent insertion and deletion. 



Problem 6:
Let's give you a word a. If another word B can be obtained by exchanging the letter order in the word, B is defined as the brother word of. Now, you can enter a word in a dictionary to find out how many brothers the word has.

Tip: Sort each word by letter, and the sibling words are sorted by the same letter (as the word signature ). Use the word signature to search for sibling words.



Problem 7:
Five barrels of balls, one bucket is abnormal. I don't know the weight and weight of the ball. I use tiantiao to find the abnormal bucket.



Problem 8:
Give two beaker with volume of M and N-litre (M! = N), there is still Endless water. What volume of water does the two beaker produce?

M, n, m + n, m-N and linear superposition

 

Problem 9:
Write an algorithm to return the largest and smallest number of given n numbers.

 

Problem 10:
Can you design an algorithm to find the largest and smallest number of sequences by performing-N comparisons? Can there be less?

Tip: first, compare them by two to distinguish the sizes into two Arrays: "big" and "small. So that the maximum number is in the "big" array, and the minimum number is in the "small" array.

 

Problem 11:
An unordered sequence composed of N-1 integers. The elements are different integers from 1 to n. Write a linear-time Algorithm for Finding Missing integers in a sequence.

Tip: Accumulate sum

 

Problem 12:
Void strton (const char * SRC, const
Char * token)
Assume that SRC is a long string of characters, and the token contains several delimiters. As long as the SRC character is any one of the token, it is split and finally SRC is split into several orders by token.
Word. Find an O (n) algorithm?

Tip: The lookup method stores all strings in an array with a length of 128 and uses it as the separator's character position 1. In this way, you can use constant time to determine whether a character is a separator, after N scans, SRC is split into words.

 

Problem 13:
An ordered array a with the length of N. Now, array a is separated from the position M (M <n, m unknown) and the two parts are swapped, assume that the new array is counted as B, and find the algorithm with the time complexity of O (lgn) to find the given number X. Is it in array B?

Tip: Binary Search is also used. The core idea is to determine the scope of the number to be searched. You can determine the range of the next search by comparing the relationship between the three numbers (header, tail, and center) and the number to be searched.

 

Problem 14:
An ordered array a with the length of N. Now, array a is separated from the position M (M <n, m is known) and the two parts are swapped, design an O (n) algorithm to implement such inversion. Only one additional space is allowed. (The efficiency of cyclic shift is not high)

Tip: (a 'B') '= ba

 

Problem 15:
A better implementation of vector is provided. (STL's vector memory doubles, but each doubling requires copying existing elements. On average, each element needs to be copied once, which is inefficient)

Tip: The fixed length of 2 ^ N can be used as the minimum unit for each allocation, and the first address of each block can be recorded in sequence. This structure can also achieve linear search, and the copy cost is very low (only pointer)

 

Problem 16:
Given the sorted arrays A and B with the length of N and m respectively, find an algorithm with the time complexity (lgn) and find the number at the K position.

Tip: Binary Search.

 

Problem 17:
Given any array A, B, with the length of n, m, respectively, please find an algorithm with the time complexity (lgn) and find the number at the K position.

Tip: the minimum number of heap records is used for updating and scanning.

This prompt indicates a problem. Find the optimal algorithm!

 

Problem 18:
Assume that array A has n elements, and the value range of the element is 1 ~ N, determine whether the array has repeated elements? The complexity is O (n ).

Method 1: use an array of N, record element, exist as 1, two appear 1, that is, repeated.

Method 2: use an array of m to record the size of N/m, 2n/m ..... Number of elements. Bucket Method

Method 3: Accumulate and sum. It can be used to find a method with only one element repeated.

 

Problem 19:
Given the sorted array A, the size is N. Given a given number X, the sum of two numbers in a is equal to X. Returns an O (n) algorithm.

Tip: search from the center to both sides. Use ordered Conditions

 

Problem 20:
Given the sorted array a with the size of N, please provide an O (n) algorithm to delete repeated elements, and no extra space can be used.

Note: Since there are duplicates, there must be redundant space. Place the elements in front of the array, record the next position that can be placed, and scan backward.

 

Problem 21:
Given two sorted arrays A and B, the sizes are N and m, respectively. An Efficient Algorithm is provided to find which elements in array a exist in array B.

Note: Binary Search is generally performed in a large array, and the elements of the small array are used as the objects to be searched.

Better Algorithm (Xuanyuan Blade
Provided ):You can use two pointers to traverse AB and compare the current size... time complexity O (N + M)



Problem 22:
Q: there are 1000 barrels of wine, one of which is toxic. Once eaten, toxicity may occur after one week. Now we use a mouse to test the experiment. We need to find the bucket of Poisonous Wine within one week and ask how many rats are needed at least.

Answer: 10. Enter the wine number as 1 ~ 1000 rename the mouse as 1 2 4 8 16 32 64 128 256 512
Make the wine number equal to the addition of the mouse number, for example: 17. Give the rats 1 and 16, and feed the rats 76, 4, 8, and 64 for seven days.
The resulting number is the toxic bucket. Because the 10th power of 2 is equal to 1024, 10 mice can test a maximum of 1024 bucket wines.

Proof: binary representation: 01, 10,100,100 0 ,... , 1,000,000,000. Any number smaller than 1024 can be represented by a unique set of binary numbers. So it was founded.

 

Problem 23:
Design a minimum number of weights so that the balance can be weighed by 1 ~ 1000 weight.

If the weight can only be placed on one side, 4,512, is the best. (Only single-plus)

If the weights can be placed on both sides, 1, 3, 9, 27 ....
Best. (Can be added or subtracted) 1. How to calculate the next number is known. The weight can be 1, 2, 3, and 4. Set the number of X, can be called weight, X-4, X-3, X-2, x-1,
X, x + 1, x + 2, x + 3, x + 4. In order to make the weight the best, the weight should not be repeated (Waste ). Therefore, x = 9. Similarly, you can get the following information.

 

Graphics algorithm questions

Problem 24:
How can I determine whether a point is within a polygon?

Tip: divide the Polygon into triangles to determine whether the points are in the triangle.

 

A very useful analytical geometric conclusion: If P2 (x1, Y1), P2 (X2, Y2), P3 (X3, Y3) are three points on the plane, the area of the triangle p1p2p3 is equal to 1/2 of the absolute value below:

| X1 Y1 1 |

| X2 Y2 1 | = x1y2 + x3y1 + x2y3-x3y2-x2y1-x1y3

| X3 Y3 1 |

When and only when point P3 is on the right side of the P1P2 straight line (with a straight line P1-> P2), the symbol of this expression is positive. This formula can check the side of a straight line at two points within a fixed period of time, and the distance from the point to the straight line (Area = bottom * Height/2 ).

 

This conclusion can be used to determine whether a vertex is in a triangle or not. Method 1: Determine whether the sum of the area of the three triangles traveling by the vertex and the three sides of the triangle is equal to the area of the original triangle. (The above formula is used ).

Method 2: Determine whether all three sides are in the same side. If the three sides are the same, the conditions are met. Otherwise, the conditions are not in the triangle.

 

Problem 25:
Two angular bisector with N as the vector and 0 points are given.

tip: normalize the two edges to get two points with a length of 1. Just take the midpoint of the two.

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.