Interview Questions of Xiaomi, Microsoft, Baidu and other companies and their promotion and Analysis

Source: Internet
Author: User

Some time ago, I saw Xiaomi, Baidu, Microsoft and other companies on the Internet have the following interview questions:

An array contains 2n + 2 integers, n of which appear twice, and only 2 appear once. You need to write an algorithm (optimal) to find these two unique numbers.

My thinking process for solving this problem is as follows:

There are two unique numbers in the original question. What if there is only one unique number? Obviously, if there is only one unique number, there is a non-General Solution (all exclusive or, the result is that unique number ). however, my idea is to use a partion sub-process of quick sorting, at the end of each partion, I assume that the k-th element is used for the position after the partion ends (the left side is smaller than the k-th element, and the right side is no smaller than the k-th element ), it knows that there is a K-1 element on the left, apparently, if the k-1 is an odd number, then the unique number we are looking for is on the left, otherwise on the right, so, at the end of each parion, I can narrow down the search space.


Thoughts:

The above algorithm seems correct, but what is its complexity? (My own algorithm, not the one-way or-out method ). what if I just removed two elements from the search space after each partion (this is the worst case, but it is entirely possible )? So the complexity of my algorithm is O (n ^ 2). It is better to sort it directly.


Analysis:

For fast sorting, I think any book will talk about the average complexity of O (nlgn), that is, the average complexity of quick sorting is based on the limit of comparative sorting (it is impossible to optimize it again ). however, if we are not lucky enough, every fast sorting will leave only one element on the left. The complexity is O (n ^ 2 ), because the probability of each occurrence is too low, the average rate of fast sorting is still relatively good (the time complexity of fast sorting is actually the average height of a binary tree, in addition, you can use a binary search tree to quickly sort one by one ).

Of course, if someone deliberately makes a mess, if someone deliberately arranges a special sequence so that each partion just splits one element, then we can only say no to the quick sorting. how can this problem be solved? Someone is smart. Preprocessing an array before sorting it quickly is a randomization (to sort the elements of the array at random ).


Problem:

Before fast sorting, randomization can only minimize the probability. However, it cannot be proved theoretically that the worst case will not happen-Because Random exchange has a probability (1/n !) The randomization sequence is exactly the same as that before randomization. In this way, the deliberate plan may still succeed.


Solution:

The reason for the above fast sorting is that it may reach O (N ^ 2) is that there is a special sequence, and the current idea is that if there is a partion, the division result cannot be C: N-C (that is, there are only limited elements on the left, but almost all elements on the right ). we can have the following policies, so that we can give a division method of 0.3: 0.7 in the worst case of the complexity of O (n, the method is as follows (on the introduction to algorithms P112)

Divide the array into five groups (less than five redundant ones are regarded as one group), sort them, and find the median of each group, the next step is to find the median (a recursive process) in the N/5 medians. The time equation is as follows: T (n) = T (N/5) + O (n), the complexity is O (n), which is known by the main theorem. That is to say, we can complete the above process in O (n) time, if the number found at the end is X, we can see that the worst proportion of partion based on X is 0.3: 0.7. That is to say, we can get a fast sort subprocess (partion) this optimized partion will make the array Proportions after the partion compare and coordinate.


Now we can solve the problem raised in the question according to the optimized partion, that is, to find two special numbers.

First, we need to prove that we can solve a special number problem in the O (n) Complexity (the clever exception or algorithm is not allowed ).

We noticed that after each partion of the array (using the optimized partion above), we get two sections. If the number of elements on the left is odd, obviously, we need to find it on the left side (the number must appear twice on the right side). Otherwise, we need to find it on the right side. the worst time complexity of the algorithm is T (n) = T (0.7n) + O (n, O (n) is required to solve a special number problem.


Now we can solve two special numbers.

Similarly, for a partion, if there are odd numbers on the left, it is known that there is a special number on the left and right. At this time, it can be solved within O (n, otherwise, if there are even numbers on the left, the numbers on the left are all different or. If the result is 0, there are two special numbers on the right. If the value is not 0, then the two special numbers are all on the left. The equation is T (n) = T (0.7n) + O (n), and the complexity of the solution is O (n ).


The preceding two special numbers are solved, and the problem is expanded as follows:

In the series of N numbers (stored in arrays and not necessarily integers), the number of T (t <k) only appears once, and the number of other M numbers appears K times, you need to write an algorithm to find this t special number. (N = T + MK)


The algorithm is as follows:

The optimized partion is still used. After a partion ends, X numbers are set on the left. If X is divisible by K, all the t numbers to be searched are on the right, at this time, the problem scale is reduced to 0.7n (at most). Otherwise, the number of T is distributed on the left and right sides, and recursive processing is performed on the left and right sides at the same time. in this way, the left and right are both linear time (O (X) on the left and O (N-x) on the Right, and the sum is also linear time (O (n )).

Conclusion: the time complexity of the algorithm is O (n), and the space is the recursive space of the O (lg n)-partion process.

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.