Interview Questions (sorted out)

Source: Internet
Author: User
1. Linear tables A and B are two linear tables in ascending order. Write a program to merge these two linear tables into an ordered linear Table H;
2. Use the four-color theorem to color n local areas. The colors are 1, 2, 3, and 4. An array of [] [N], for example, if adj [I] [J] = 1, it indicates that area I is adjacent to area J, array color [N], such as color [I] = 1, indicates that the color of area I is the 1 color.
3. Use recursive algorithms to determine whether array a [n] is an incremental array.
4. Write an algorithm and select the largest 1 billion floating point numbers from the 10000 floating point numbers.
5. Write a UNIX program to prevent zombie processes.

I am applying for a search engine engineer for four interview questions. The last two questions are extremely difficult (I hope you can give me more calculations)
1. Give two arrays and their sizes, as well as a dynamic memory, calculate the intersection, place the intersection to the dynamic memory Dongtai, and return the number of intersections
Long jiaoji (long * A [], long B [], long * alength, long blength, long * Dongtai [])
2. Create a single-connection table. Insert the 26 letters 'A' -- 'Z' into the table with flashback and print it!
3. A terrible question has finally arrived.
The input information of the search is a string, and the top 10 of the top 10 input information is counted. Each time we input a string of no more than 3 million bytes, the memory usage is only 1 GB,
Describe the concept, write computation (C), space and time complexity,
4. in China, there are hundreds of thousands of topics, such as Baidu. Suppose every topic has hundreds of millions of posts. How can we design the system with the best speed? Describe the idea, write computation (C), space and time complexity,

I'm not sending any code, but there are all these questions. I'll tell you the title.

1. Linear tables A and B are two linear tables in ascending order. Write a program to merge these two linear tables into an ordered linear Table H;
The answer lies in the example in chapter 2 of "Data Structure Second Edition" by Yan Ruimin of Yinghua University)

2. Use the four-color theorem to color n local areas. The colors are 1, 2, 3, and 4. An array of [] [N], for example, if adj [I] [J] = 1, it indicates that area I is adjacent to area J, array color [N], such as color [I] = 1, indicates that the color of area I is the 1 color.
The answer lies in a set of foreign data structure textbooks introduced by China Water Conservancy press, a blue envelope (this set of books includes the Operating System (using minux) and multimedia, estimated to be years old)

3. Use recursive algorithms to determine whether array a [n] is an incremental array.
I did not know this before, but it's not hard !!
Post Code later

4. Write an algorithm and select the largest 1 billion floating point numbers from the 10000 floating point numbers.
Sort data externally. This is in the data structure book !!!

5. Write a UNIX program to prevent zombie processes.
Are you talking about zombie processes as deadlocks? UNIX program I won't

1. Give two arrays and their sizes, as well as a dynamic memory, calculate the intersection, place the intersection to the dynamic memory Dongtai, and return the number of intersections
Long jiaoji (long * A [], long B [], long * alength, long blength, long * Dongtai [])

I did not know this before, but it's not hard !!
Post Code later

2. Create a single-connection table. Insert the 26 letters 'A' -- 'Z' into the table with flashback and print it!
This is hard to understand.

3. A terrible question has finally arrived.
The input information of the search is a string, and the top 10 of the top 10 input information is counted. Each time we input a string of no more than 3 million bytes, the memory usage is only 1 GB,
Describe the concept, write computation (C), space and time complexity,
Terrible indeed,

4. in China, there are hundreds of thousands of topics, such as Baidu. Suppose every topic has hundreds of millions of posts. How can we design the system with the best speed? Describe the idea, write computation (C), space and time complexity.
Terrible indeed,

1. Linear table A and B are two linear tables in ascending order. Write a program to merge these two linear tables into one.

Two-way merger, not difficult
2. Use the four-color theorem to color n local areas. The colors are 1, 2, 3, and 4. An array of [] [N], for example, if adj [I] [J] = 1, it indicates that area I is adjacent to area J, array color [N], such as color [I] = 1, indicates that the color of area I is the 1 color.
For the graph theory of convertible bits, each region is regarded as a point on the graph. adjacent points are connected to a line to form an undirected graph. The adjacent matrix is obtained, and the color numbers are obtained based on the adjacent matrix.
3. Use recursive algorithms to determine whether array a [n] is an incremental array.
Upstairs

4. Write an algorithm and select the largest 1 billion floating point numbers from the 10000 floating point numbers.
You can select a 1 billion-digit number from the first 10,000th floating point numbers and set the number of bits to M. In the selection of the base values of M bits, you can use a quick sort.
The subsequent number is required.

1. Give two arrays and their sizes, as well as a dynamic memory, calculate the intersection, place the intersection to the dynamic memory Dongtai, and return the number of intersections
Long jiaoji (long * A [], long B [], long * alength, long blength, long * Dongtai [])
I think of the brute force method, time complexity bit O (alength * blength); I think everyone knows it!

2. Create a single-connection table. Insert the 26 letters 'A' -- 'Z' into the table with flashback and print it!
It is not difficult to reverse the order of a single-chain table.

3. A terrible question has finally arrived.
The input information of the search is a string, and the top 10 of the top 10 input information is counted. Each time we input a string of no more than 3 million bytes, the memory usage is only 1 GB,
Describe the concept, write computation (C), space and time complexity,
255byte * 3 million <1g; that is to say, all input information can be transferred. The PageRank algorithm can be used for frequency statistics and sorting.
Note: The PageRank algorithm is patented by Google and cannot see the source code.

1. Linear tables A and B are two linear tables in ascending order. Write a program to merge these two linear tables into an ordered linear Table H;
Known: A [0] <A [1]... a [n-1] <A [n];
B [0] <B [1]... B [n-1] <B [N];

I. A [n] <B [0]
II. B [N] <A [0]
Iii. Insert sorting & Binary sorting
# Include "stdio. H"
# Include "conio. H"

Main ()
{
Int I, J, K;
Int H [20];
Int A [10] = {2, 5, 6, 9, 11, 24, 56, 78, 80, 81 };
Int B [10] = {, 21 };
I = 0;
J = 0;
For (k = 0; k <20; k ++)
If (A [I]> B [J])
{H [k] = B [J]; j ++ ;}
Else
{H [k] = A [I]; I ++ ;}

For (I = 0; I <20; I ++)
Printf ("% d", H [I]);

Getch ();

}

// Create a single-connection table. Insert the 26 letters 'A' -- 'Z' into the table, reverse the description, and print it!

Node * P = NULL;
Node * q = NULL;

Node * head = (node *) malloc (sizeof (node ));
Head-> DATA = ''; head-> next = NULL;

Node * First = (node *) malloc (sizeof (node ));
First-> DATA = 'a'; first-> next = NULL; head-> next = first;
P = first;

Int longth = 'Z'-'B ';
Int I = 0;
While (I <= longth)
{
Node * temp = (node *) malloc (sizeof (node ));
Temp-> DATA = 'B' + I; temp-> next = NULL; q = temp;

Head-> next = temp; temp-> next = P; P = Q;
I ++;
}

Print (head );

In fact, no one on the top of the fourth question is right.
Select from 1 billion. In fact, there are many numbers. Of course, external sorting is a method.
It is estimated that you want to test whether you will have a binary sorting tree.
The maximum number of 10000 nodes is 10000. This memory can be mounted.
However, the number of data entries must be read from external files. Every time you read A/group, you try to put it into the sorting tree. When 1 billion data entries are fully read, the 10000 data entries are output in the middle order.
I have also seen similar questions in csdn, and the answer is also a binary sorting tree, as if no one answered.

The input information of the search is a string, and the top 10 of the top 10 input information is counted. Each time we input a string of no more than 3 million bytes, the memory usage is only 1 GB,
Describe the concept, write computation (C), space and time complexity,
This question is not very difficult. Use the hash method to hash these strings into different buckets, and then determine the maximum number of strings.

My program for question 4th

# Include <stdio. h>
# Include <iostream>
# Define Max 1000000 // The total number of settings. I set it to 1 million or 10 million here. If it is 0.1 billion, the memory will not be able to stand, but my program reads data in serial mode, therefore, it can also process larger data, which is put in data for convenience.
# Define top 1 1000

Void quicksort (INT array [], int left, int right)
{
If (left <right)
{
Int DIV = array [right];
Int I = left;
Int J = right-1;
Int temp;
If (I = J) {// there are only two pieces of data, and the processing is complete.
If (array [I] <Div)
{
Array [I + 1] = array [I];
Array [I] = div;
Return;
}
}
Else
{
While (I <j)
{
For (; array [I]> div; I ++ );
For (; array [J] <= div; j --);
If (I> = J) break;

Temp = array [I];
Array [I] = array [J];
Array [J] = temp;

}

If (I! = Right)
{
Array [right] = array [I];
Array [I] = div;
}
Quicksort (array, left, I-1 );
Quicksort (array, I + 1, right );

}
}
}

Int main ()
{

Int * big = new int [Max];
Int * psort = new int [top * 4]; // The best result is psort [0]-psort [Top-1 ].
Int I = 0, j = Top, K = 0;
Int h = 0;
Int all = 0; // Big Data counter
Srand (Time (null ));
For (I = 0; I <Max; I ++)
{
// Int JJ = rand () % 77 + 1;
// Int nn = rand () % JJ;
Big [I] = rand ();
// Cout <big [I] <"";
}

// You can add some test data to check whether the big data can be ranked first.
Big [0] = 100000;
Big [10] = 300002;
Big [11] = 200002;
Big [MAX-2] = 200007;

Int temp = big [0];

For (all = 0; all <top; All ++)
{
Psort [all] = big [all];

}
Quicksort (psort, 0, top-1 );

// For (k = 0; k <top-1; k ++) cout <psort [k] <"";
// Cout <Endl;
Temp = psort [Top-1];
I = Top-1;
J = top * 2;

Int sum = 0;

For (all = top; all <Max; All ++)
{
Sum ++;
If (BIG [all]> temp)
{

Sum ++;

Psort [I ++] = big [all];

If (I = top * 2) // I is full top * 2
{
Sum + = 3;
I = 0;
J = top * 2;
Int temp2 = psort [top * 2-1];

For (INT n = 0; n <top * 2-1; n ++)
{
Sum + = 2;

If (psort [N]> temp2)
{
If (I! = N) psort [I] = psort [N]; // place large values in front of psort
I ++;
}
Else
{
Psort [J ++] = psort [N]; // a small part is placed after psort.
}

}

If (I <top) // I <100, the number of splits is relatively large, AND THE NUMBER AFTER 100 is reselected as the number of splits.
{
While (I <top)
{
Sum + = 5;

Psort [I ++] = temp2;
J --;
Temp2 = psort [J];
K = top * 2;
H = top * 2;
For (; k <j; k ++)
{
Sum + = 2;

If (psort [k]> temp2)
{
Psort [I ++] = psort [k];
}
Else
{
If (H! = K) psort [H] = psort [k];
H ++;
}
}
J = h;
} // I> 100. After selecting the number of shards after 100
}
Sum ++;
Temp = temp2;
}
}
}

Cout <Endl <sum <"" <I <"" <j <Endl;

////// This is the top one of the output in full sorting. Check whether the result is the same as this one, but the result can be executed only when the max value is small, otherwise, Stack Overflow /////
/*
Quicksort (big, 0, MAX-1 );

For (k = 0; k <top; k ++) cout <big [k] <"";
Cout <Endl;
Cout <Endl;
*/
//////////////////////////////////////// //

// Output the top one
Quicksort (psort, 0, I-1 );
For (k = 0; k <top; k ++) cout <psort [k] <"";

Getchar ();
Delete [] Big;
Delete [] psort;
Return 0;
}

It is not difficult to answer questions about algorithms.
Let me give you some ideas:
1. Linear tables A and B are two linear tables in ascending order. Write a program to merge these two linear tables into an ordered linear Table H;
This question is essentially the same as the first question below, I =.

3. Use recursive algorithms to determine whether array a [n] is an incremental array.
This is to narrow down the scale of the last one and the boundary each time.

4. Write an algorithm and select the largest 1 billion floating point numbers from the 10000 floating point numbers.
External sorting

5. Write a UNIX program to prevent zombie processes.
It has nothing to do with algorithms. I won't.

1. Give two arrays and their sizes, as well as a dynamic memory, calculate the intersection, place the intersection to the dynamic memory Dongtai, and return the number of intersections
Sort O (N * log (N) first, and then perform the same steps as the first question. This question is: Take the Small Header, compare, the same into the memory, not the same as the small throwing O (n) completed. Add the sorting O (N * log (n.
I don't need to talk about the above questions. It's just better than merging.

2. Create a single-connection table. Insert the 26 letters 'A' -- 'Z' into the table with flashback and print it!
Linked lists are basic functions.

The input information of the search is a string, and the top 10 of the top 10 input information is counted. Each time we input a string of no more than 3 million bytes, the memory usage is only 1 GB,
Describe the concept, write computation (C), space and time complexity,
I don't know. However, you will know that the space is sufficient: All loads are sufficient (of course we will not do this)
4. in China, there are hundreds of thousands of topics, such as Baidu. Suppose every topic has hundreds of millions of posts. How can we design the system with the best speed? Describe the idea, write computation (C), space and time complexity,
I don't know. Did not learn living knowledge

A few friends upstairs sorted back. Let's discuss a problem.
The result requires 10000 ordered numbers, and you are not required to sort all 1 billion numbers.
So the more efficient approach should be:
1. The number of read headers is 10000, and a binary sorting tree is directly created. O (1)
2. Check whether the number of entries to be read in the future is smaller than that of the first 10000. (N comparisons) if small, read the following number. O (N)
3. If it is large, search for the binary sorting tree and find the location where it should be inserted.
4. Delete the smallest node.
5. Repeat Step 2 until all the 1 billion data entries are read.
6. traverse and output all 10000 numbers in the current binary sorting tree in the middle order.
Basically, the time complexity of the algorithm is O (n) times.
The algorithm's spatial complexity is 10000 (constant)

Let me answer the following two questions:
1. Recently, companies have found the most frequent first few or repeated entries from millions of entries.
In fact, this problem does not require much so-called search expertise. It is not difficult to think about how the database is implemented or how the dictionary index chapter in the data structure is located. The solutions to these questions are similar, two data structures are used:
Tree (Binary Tree, trie tree and B tree) and hash table. For the 3 million entry, building an open address hash certainly won't use 1 GB of memory. If you are not at ease, you can build a trie tree, if you are afraid that the memory is not enough, you can build a B + tree for multi-level indexing. For complexity analysis, let's look back at your data structure.
2. The design similar to the post bar is nothing more than a multi-level index, query and update problem.
Level 1 is a topic. More than 0.1 million topics are directly hashed for efficiency. During query, these topics must be fully loaded into the memory;
The secondary index is hundreds of millions of data records. The memory cannot be stored during queries or updates. Naturally, the B + tree is required. I just want to add that I want to use the B + tree to load data hierarchically when the memory cannot load all the data, in this way, we can take full advantage of memory swap to solve the problem of massive data operations. This is similar to the file system design of the operating system and the database implementation principle.

4. Write an algorithm and select the largest 1 billion floating point numbers from the 10000 floating point numbers.

Remember, the time complexity of the Sorting Algorithm is O (n * log n), which is the fastest
However, the time complexity here is O (n * log n ),
Where N is 1g, n is 10 K, probably: 30/13
In addition, this place does not need to use time-consuming sorting.
For general systems with far slower disk access than memory access, the efficiency is not an order of magnitude.

Class csearch
{
Public:
Void input (float F)
{
If (m_vbuf.size () <10000 | f> * m_vbuf.begin ())
{
M_vbuf.insert (f); // log (2, n) time complexity
If (m_vbuf.size ()> 10000)
M_vbuf.erase (m_vbuf.begin ());
}
}

STD: Set <float> m_vbuf;
};

The rest is to repeat all the data 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.