Find the missing and repeated data in the array

Source: Internet
Author: User
Tags bitset

20130506 Microsoft intern interview questions:

The size of an array is N, and the element size range is 1-n. Some of them are missing, while others are repeated. First, we need to find the number of duplicates and the number of missing ones.



I hardly thought about it. I wrote it quickly:

/************************** A [n], element range: 1-N * Number of duplicate and lost outputs **************************/void FA (int A [], int N) {int * P = new int [n + 1] (); For (INT I = 0; I <n; I ++) {P [A [I] ++;} set <int> M, R; // Save the number of lost and repeated values, you can also directly output for (INT I = 1; I <= N; I ++) {If (P [I]> 1) R. insert (I); else if (P [I] = 0) M. insert (I);} Delete [] P ;}

The interviewer said, how complex are you?

I said that the time complexity is O (n). Because the auxiliary array is used, the space complexity is also O (n ).


"If the array is large and requires too much auxiliary space, you need to optimize it to reduce the space complexity to O (1). How can this problem be solved ?"

I thought for a few seconds and couldn't help with the space, so there may be losses in that time. Let's just say, "Let's sort it. Just traverse it once ."

At this time, the time complexity is preferably O (nlogn), and heap sorting is used. (The complexity of the quick sorting time is not O (1), preferably O (logn), or O (n ))


Obviously the interviewer is not satisfied... I am not satisfied with myself. algorithms cannot sacrifice time ....

The time O (N) and space O (1) are required, he said ).


Then I thought about how to use bitmap. He asked me to write it out,

Then I changed the above array to a bit storage. The tragedy is that bitset has not been used for a long time and basic operations defined by bitset cannot be remembered at the moment.

I wrote it roughly and explained that I forgot the bitset. If there is a manual, I can write it right away.

The interviewer said that you don't need STL. Use the memory on your own.

I thought about it. I usually use an int to represent multiple digits. I didn't apply for a piece of memory in this case. How can I perform a bit operation if I apply for a large piece of memory at a time?

What's more, I told my thoughts. He asked me to write an int bit operation. I wrote it out. Did he say that the multiple int types are not similar?

I didn't expect to move the pointer at the time... He had to struggle with him many times before he said...

(In the past, there were too few project experiences, and I had never done it before. I thought about it on the spot... It is not smooth... Especially in the tense interview atmosphere)

Finally, he said, you can write a bitmap class by yourself to implement bitset operations in STL, such

Bitmap BM (30 );

BM [2] = 1;

BM [5] = 0;

This operation.

It was troublesome to write at the beginning... : If a vector or something has been written, reload [] to return the reference of the element. In this way, values can be assigned.

But bit reference, it seems that I have never met ?? What to do ....

I had to use the function type to write the Set () and reset () interfaces. Although it is equivalent to the above assignment, it does not meet his requirements after all.

Then he looked at it and said, "Is that what you wrote ?"

I have to say that the overload operator is not sure ....

Then he said, "As an engineer, It is very common to write code ......"

I know this interview has ended.



I will pack up my work right away. I will go to SH for an internship the next day.

After a few days, I asked several people around me.

Then, on the Internet, you can see the following algorithms written by others:

Void fun () {int A [] = {-1, 3, 3, 3, 3, 3, 8, 9, 8}; // A [0] NOT use int n = sizeof () /sizeof (INT)-1; for (INT I = 1; I <= N; I ++) {While (A [A [I]! = A [I]) {int T = A [I]; A [I] = A [T]; A [T] = T ;}} for (INT I = 1; I <= N; I ++) {if (a [I]! = I) {cout <I <"\ t"; // Number of dropped cout <A [I] <Endl; /// Number of duplicates }}}

It seems to me that I bought an English version of this book on programming Pearl River, but I didn't see it ....

Alas, tragedy.

Summary: I have met several companies, basically some conventional algorithms. I have seen them, and I have answered them very quickly. In addition, there is little code written on the site, basically just a line of thinking. Then, I will ask some language, OS, network, and other knowledge points. Some of them won the game and got the offer, but there are also some tragedies. Almost all of them are partial algorithms. There are very few scenes I have never seen.

The difficulty of Microsoft is that, whatever you say, I will let you write code. It is even more tragic... To write a scheduling program, I wrote the data structure and the interviewer said, "That's good. Now you can write the code you just mentioned. Arbitrary language.


My roommate was much luckier than I was during the interview. The algorithm for the two sides was to write Bubble sorting!

Of course there are other problems on one and three sides. He has now obtained an offer verbal transfer, but it's not enough to go.


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.