Common algorithm problems during interviews

Source: Internet
Author: User
Problem1: Isitaloop? (Judge whether the linked list has a ring ?) 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. Problem2: design an algorithm with the complexity of n to find the m element at the bottom of the linked list. The last element is assumed to be the reciprocal of 0TH. Submit

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 to 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 with 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 an algorithm with the complexity of n to find 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 sugar spoon to a spoonful of sugar to the salt, stir evenly, then scoop a spoonful of mixture will put sugar in the beaker, ask you which two beaker has more 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: Give you two files a and B, each containing 5 billion URLs, each occupying 64 bytes, the memory limit is 4 GB, let you find the common url of file a and file 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: 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 can use tiantiao to find the abnormal bucket.

Problem 8: Give two beaker, volume is m and n litre (m! = N), there is still endless water. what volume of water does the two beaker produce?

A combination of m, n, m + n, m-n, and linear superposition.

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

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 consisting 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 and 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 separators. as long as the src character is any one of the token, it is split, finally, src is divided into several words by token. 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: A sorted array A with the length of n. now, the array A is taken from the position m (m

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: A sorted array A with the length of n. now, the array A is taken from the position m (m

Tip: (A 'B') '= BA.

Problem 15: Provides a better implementation of Vector. (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: The sorted arrays A and B are given 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.

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, with the size of n. Given the 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 duplicate elements without extra space.

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.

Problem 22Q: 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 when you feed the mouse, make the wine number equal to the addition of the mouse number, for example: the 17-day wine is fed to the 1-day and 16-day mice. the 76-day wine is fed to the 4-day, 8-day, and 64-day Mice. after seven days, the numbers of the dead mice are added to the poisonous ones. because the 10th power of bucket 2 is equal to 1024, a maximum of 1024 bucket wines can be tested by 10 mice.

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.

Problem 24: How can I determine whether a vertex is in 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: Returns two angular bisector with n as the vector and 0 points.

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

Address of this article: http://www.nowamagic.net/librarys/veda/detail/233. welcome here.

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.