Baidu interview questions are for some reference only.

1. Complete the Function

Size_t Foo (unsigned int * A1, size_t al1, unsigned int * A2, size_t Al)

Both A1 and A2 are unsigned arrays, al1 and Al are the length of arrays, and the length of arrays is an even number.

An unsigned array consists of a number range. For example:

A1 is 0, 1, 3, 6, 10, 20

A2 is 0, 1, 20, 50, 4, 5

A1 indicates the following range: [] [] []

A2 indicates the following range [] [] []

The overlapping part of A1 and A2 is [] [], and its length is 2.

The Foo function must return the length of the overlap interval. 2 In the preceding example.

Requirements:

Describe in detail about your own solutions and some key points of your implementation.

Write out the original code of function Foo. In addition, the efficiency is as high as possible, and the Code complexity analysis is given.

Restrictions:

The length of al1 and Al 2 cannot exceed 1 million. In addition, the intervals of the same Array may overlap.

For example, A1 may be 9,100, or 80.

The storage space is as small as possible.

Solution: sort the intervals of two arrays first. Sort the first number of intervals as the key, and then construct a binary sorting tree for an array, search the intervals in another array in the binary sorting tree. Time complexity (nlogn ).

Someone proposed the complexity of log (+ n), that is, to sort the two arrays. The sorting rules are as follows. Then, the two arrays are merged and the result is the result.

Two people are in a queue. We think that the sequence from low to high is correct, but some people do not follow the order. If we say that the people in front are higher than those in the back (the two are the same height and think it is appropriate), we think that these two people are "troublemakers", for example, there is a sequence:

176,178,180,170,171

<176,170>, <176,171>, <178,170>, <178,171>, <180,170>, <180,171>,

So, now we provide an integer sequence. Please find out the number of these troublemakers (only the number of troublemakers is given, without a specific pair)

Requirements:

Input:

Is a file (in), each row of the file is a sequence. The sequences are all numbers separated by commas.

Output:

It is a file (out) with a number for each behavior, indicating the logarithm of the troublemakers.

Describe in detail about your own solutions and some key points of your implementation. The implementation code is provided, and the time complexity is analyzed.

Restrictions:

The maximum number of numbers in each line is 100000, and the maximum number is 6 digits. There is no memory usage limit for the process sequence.

Basic Idea: divide the entire sequence into multiple incremental sequences, and then start from the second sequence, compare each number with the previous sequence, the time complexity is

2. The following two questions are selected. Please answer one question based on your situation (4th answers for the Web direction and 3rd answers for other positions ).

3

Consider an online friend system. The system maintains a friend list for each user. The list can contain a maximum of 500 friends. Friends must be other users in the system. Friend relationships are unidirectional. User B is a friend of user a, but user A is not necessarily a friend of user B.

The user is represented as an ID. The text format of the friend list data is as follows:

2 567,890

31, 66

14 567

78 10000

...

Each row has two columns. The first column is the user ID and the second column is the friend ID. Different IDs are separated by commas (,), and IDs are sorted in ascending order. Columns are separated by "T.

Requirements:

Design an appropriate index data structure to complete the following queries:

If user a and user B are given, check whether there is such a relationship between user a and user B: B is a two-dimensional friend of user a (friend's friend ).

In the preceding example, 10000 is a two-dimensional friend of 1, because 10000 is a friend of 1 and is a friend of 78.

Describe in detail about your own solutions and some key points of your implementation. The pseudo-code is provided to implement the indexing and query processes, and the space and time complexity are described.

Restrictions:

The number of users is no more than 10 million, with an average of 50 friends.

4

Link mode: user (userid, username), article (ArticleID, userid, title, content), vote (ArticleID, score), and user is a user relationship, article indicates the relationship between the articles published by the user, vote indicates the relationship between the votes received by the articles, title indicates the article title, and score indicates the number of votes received.

(1) query all user names that have not published any articles in SQL;

(2) query the titles of all articles with more than 100 votes in SQL, and sort them in reverse order;

(3) use SQL to query the user names whose number of articles in the departure table is greater than 5 and whose average number of articles is greater than 100, sorted in descending order by average number of votes;

(4) design the primary keys, foreign keys, and indexes of these tables, and point out the indexes used for the preceding three queries.

(5) When the number of users exceeds 10 million and the number of articles exceeds 0.1 billion, how should we consider storage and performance improvement and optimization?