2011.10.17 Baidu Noodle Test
1, process switching need to pay attention to what problems.
Save the value of the processor PC register to the private stack of the aborted process, save the value of the processor PSW register to the private stack of the aborted process, and save the value of the processor SP register to the Process control block of the aborted process;
Save the value of the processor's other registers to the private stack of the aborted process; The process control block from the running process takes the SP value and stores the processor's register SP; the private stack of the running process recovers the value of the processor registers;
The PSW value is ejected from the private stack of the running process and fed into the processor's PSW, and the PC value is ejected from the private stack of the running process and fed into the processor.
2. Enter an ascending array, and then quickly look for two numbers in the array, which equals a given value.
This programming beauty has this topic, very simple, with two pointers to a pointer to the front of the array, a pointer to the back of the array, iterate over it.
3, there is a celebrity and a lot of civilians in a piece, the common people know this celebrity, but this celebrity does not know any civilian, any two civilians whether the understanding is unknown, please design an algorithm, quickly find a celebrity in this person. Known to have implemented a function bool Know (int a,int b) When this function returns True, indicates that a recognizes B and returns false when a does not recognize B.
Idea: First of all the N-person into the N/2 group, each group has 2 people, and then each group of two people call this know function, assuming that know (a, A, b), return True when the description of a to know, a is not a celebrity, a can be ruled out, and so on, each group calls this function sequentially, Then there are n/2 individuals who are excluded from N, and the data size will be N/2. Similarly, in the remainder of the N/2 personal use of this method, then the scale will be N/4, so all the number of traversal is n/2+n/4+n/8+ ... The time complexity of this one geometric series is O (n).
4, judge whether a natural number is a number of squares. Of course, you can't use a root operation.
Method 1:
Traverse a number from 1 to N to take the square and compare it with N.
If the square is less than N, the traversal continues, and if it equals N, the exit succeeds, or if it is greater than N, the failure exits.
The complexity is O (n^0.5).
Method 2:
Using the binary lookup method, the numbers between 1 and N are judged.
The complexity is O (log n).
Method 3:
Because
(n+1) ^2
=n^2 + 2n + 1,
= ...
= 1 + (2*1 + 1) + (2*2 + 1) + ... + (2*n + 1)
Note that these items constitute a arithmetic progression (2 difference between each item).
So we can compare N-1, N-1-3, N-1-3-5 ... and a 0 relationship.
If it is greater than 0, the decrease is continued, if it equals 0, the exit succeeds, or if it is less than 0, the failure exits.
The complexity is O (n^0.5). However, Method 3 uses addition and subtraction to replace the multiplication in method 1, so the speed is faster.
For example: 3^2 = 9 = 1 + 2*1+1 + 2*2+1 = 1 + 3 + 5
4^2 = 1 + 2*1 + 1 + 2*2+1 + 2*3+1 view plain Int square (int n) { &nbs p; int i = 1; n = n - i ; while ( n > 0 ) { i += 2; n -= i; } if ( n == 0 ) //is a number of squares return 1; else //is not a square of a number return 0; }
Baidu 2011.10.16 Campus Job Fair Pen Test
First, algorithm design
1. Set rand (S,T) to return a random decimal between [s,t], using the function to find random n points within a circle with a radius of R, and give a time complexity analysis.
Idea: This use of mathematical polar coordinates to solve, first call [S1,T1] randomly generated a number r, normalized multiplied by the radius, get r* (R-S1)/(T1-S1), and then call [S2,T2] randomly generate a number A, normalized after the angle: 360* (A-S2)/ (T2-S2)
2, in order to analyze user behavior, the system often need to store some of the user's query, but because of a lot of query, so the system can not be all, set the system to save only m query per day, now design an algorithm, the user request for a random selection of M, please give a program, So that each query is pumped in the same probability, and analysis, note: Less than the last moment, do not know the total amount of user requests.
Idea: If the number of users query is less than m, then directly stored up. If the number of user queries is greater than m, assuming that m+i, then 1-----m+i randomly generated a number, if the choice is the front m query for access, then the probability is m/(m+i), if the selection is the query in the later I record, Then the probability of replacing the previous m-Log query record with this record is m/(m+i) * (1-1/m) = (m-1)/(M+i), when the query record volume is very large, m/(m+i) = = (m-1)/(M+i), so each query is pumped in the probability is equal.
3. Problems related to vector in C + + STL:
(1), when calling Push_back, its internal memory allocation is how.
(2), when you call clear, the internal is how to implement the concrete. What to do if you want to release its memory.
Vector works by pre-allocating a capacity-sized space, and when the inserted data exceeds this space, the space is extended in some way, but when you delete the data, it does not shrink.
Vector in order to prevent the overhead of allocating a large amount of contiguous memory, to maintain a default size of memory, clear is only clear data, not clear memory, because the vector's capacity capacity has not changed, the system maintains a default value.
Is there any way to release all the memory used in the vector?
The standard solution is as follows
Template < class T >
void Clearvector (vector< T >& VT)
{
vector< T > vttemp;
Vetemp.swap (VT);
}
In fact, the vector simply does not care about memory, it is only responsible for acquire/release memory to the memory management framework, the memory management framework if it finds that memory is not enough, malloc, but when the vector frees up resources (such as destruct), STL simply does not call free to reduce memory, because memory is allocated to the bottom of the STL: STL assumes that if you need more resources you will probably need so many resources later (your list, HashMap also use these memory), so there is no need to keep malloc/free. If that's the logic, it could be a trade-off.
The general STL memory manager allocator uses memory pools to manage memory, so a container that requests memory or frees memory only affects the amount of memory remaining in the pool, rather than actually returning the memory to the system. This is done to avoid memory fragmentation, and the second is to increase the efficiency of memory requests and releases--without having to look in the system memory every time.
Second, the system design
The normal client sends a maximum of one request per minute to the service side, the server needs to do a filtering system for abnormal behavior of clients, and the servers will receive a request from client A at a certain moment, then any other requests from clients within 1 minutes need to be filtered, it is known that each client has an IPV6 address as its ID. The number of clients is too many, so that can not be placed in a single server memory hash table, it is necessary to design a system, using support efficient filtering, can use more than one machine, but requires fewer machines the better, please use the key design and ideas in the chart and code to show.
Three, to find a full array of functions:
such as P ([+]) output:
[123], [132], [213], [231], [321], [323]
Find a combination function
such as P ([+]) output:
[1], [2], [3], [up], [2,3], [1,3], [All-over]
These two questions can be used in pseudo-code.
NetEase Games 2011.10.15 Campus Job Fair Pen Test
1, for a memory address is 32-bit, memory page is 8KB system. 0x0005f123 What is the page number of this address and the offset in the page?
2. If x is greater than 0 and less than 65536, the value of x multiplied by 255 is calculated by the shift method: (x<<8)-X
X<<8-x is not correct because the priority of the shift operator does not have a high priority for the minus sign, and first calculates whether the 8-x is 0,x left 0 bits or 8.
3, a four-fork tree containing n nodes, each node has four pointers to the child node, which has 3n+1 null pointer in the 4n pointer.
4, the following two statements the difference is: the first dynamic application of the space inside the value is a random value, the second is initialized, the value is 0 view plain int *p1 = new INT[10]; int *p2 = new INT[10] ();
5, the computer in memory storage data use large, small end mode, please write separately a=0x123456 in different cases of the first byte is, big-endian mode: 0X12 small end mode: 0X56 X86 structure of the computer using the small terminal mode.
In general, most users ' operating systems (such as Windows, Freebsd,linux) are in small-ended mode. Small parts, such as Mac OS, are big-endian mode.
6, in the game design, often according to different game state call different functions, we can use function pointers to achieve this function, declare a parameter is an int *, return the value of int function pointer:
Int (*fun) (int *)
7, the following program runs after the result is: To test something view plain char str[] = "Glad to test something";