• Handwritten Code Topic 1:
    /* * There are n rooms in room I, and now the people in room I need to be reassigned. The allocation rules are as follows: first let all the people in room I out, * then I +1, I +2, I +3... Put one person in each of these rooms in that order, and the next room in n is room 1, until all the people have been reassigned. * Now tell you the number of people in each room after the assignment and the room number x for the last person. You need to figure out the number of people in each room before the assignment. Data guarantee there must be a solution, if there are multiple solutions output any one solution. * * * first came up with the idea of blasting: start from the room where the last person was assigned and work backwards, subtracted one person from each room until the number of people in a room was reduced to zero, but that was not the optimal solution... * After the interview, I came up with another solution: the minimum number of people in each room after the assignment is the initial room I. The minimum number of people is the number of rounds reassigned. * */ function findRoom($n, $x, $room) { $min = min($room); $index = function ($i) use($n) { return $i >= 0? $i % $n:$i % $n + $n; }; For ($I = $x - 1, $d = 0; $min ! = $room[$index($i)]; $i --, $d ++ ) { $room[$index($i)] -= $min + 1; $room[$index($I)] = $min * $n + $d; if ($index($i) ! For ($I = $I - 1; $index($i) ! = $x; $i --) { $room[$index($i)] -= $min; } $room[$index($i)] -= $min; } var_dump($room); }Copy the code
  • Handwritten code problem 2

    /* * Print a binary tree to see nodes from the left view * Given a normal binary tree, print the nodes that can be seen from the left view of the binary tree * for example, a normal binary tree * 1 * / \ * 2 3 * / \ / \ * 4 5 6 7 * /* 8 * When viewed from the left, print the nodes that can be seen 1, 2, The four nodes, four nodes, eight nodes. Class Node{public $val, $left, $right; public function __construct($val) { $this->val = $val; } } function leftView($root) { $stack = []; $stack[] = $root; $ret = []; while (! empty($stack)) { foreach ($stack as $eachNode) { $leftNode = array_shift($stack); if (! empty($leftNode->right)) { $stack[] = $leftNode->right; } if (! empty($leftNode->left)) { $stack[] = $leftNode->left; } } $ret[] = $leftNode->val; } var_dump($ret); }Copy the code
    • The advantages and disadvantages of Redis?
      • The above two algorithms are basically not done, resulting in a collapse of mentality, so the answer is not very good, the advantage is around Redis several features and data structure and its application scenarios, persistence, sentry, cluster to explain, the disadvantage is only said that it can not fully fault tolerance and recovery, the following carefully recorded learning.
      • Advantages:
        1. Data stored in memory, read and write speed is very fast, using a single thread structure, to prevent the possible problems and consumption due to multithreading competition.
        2. Rich data types, five basic data types, and many useful extended data types, such as bitmap, HyperLogLog, publish and subscribe, GEO, etc.
        3. Rich features, providing key expiration, simple transactions, Lua script management, Pipeline and other functions.
        4. Data can be persisted through RDB and AOF.
        5. Providing master-slave replication, leveraging clusters, sentinels to build highly available distributed architectures.
      • Disadvantages:
        1. There is no automatic fault tolerance and recovery function.
        2. When the host is down, data that has not been synchronized to the slave host cannot be recovered.
        3. Primary/secondary replication adopts full replication. If the snapshot file size is too large, the cluster service capability will be greatly affected.
        4. Online capacity expansion is not supported.
  • Deadlock conditions:

    1. The mutex request
    2. Can’t be deprived
    3. Loop waiting for
    4. The request to keep
  • The difference between a process and a thread

    • Processes are the basic unit of resource ownership, and threads are the basic unit of CPU scheduling and dispatching.
    • A program has at least one process, and a process has at least one thread.
    • The process has a separate memory unit in the execution program, while multiple threads share the memory, thus greatly improving the efficiency of the program.
    • Processes have independent address Spaces, a crash of one process in protected mode does not affect other processes, and threads are just different execution paths within a process. Thread has its own stack and local variables, but there is no separate address space between threads, a thread dead is equal to the whole process dead, so multi-process procedures than multithreaded procedures robust, but in the process switch, the cost of resources is larger, the efficiency is poor.
  • Let’s say you have 50M of data now, and the network latency is 50ms, and just consider TCP, please estimate how long it will take to send this data from Beijing to Shenzhen.
    • After I… Look confused ah. I then went through the whole process of TCP sending data from the client to the server, and felt that the interviewer was not satisfied.
    • I feel that the test point of this problem is that the data will be segmented in TCP transmission, and the maximum length of each section is limited. Subtracting the length of the TCP header is the length of the data that can be sent in each section, and then I am calculating, but at that time my mind has been broken by the algorithm, so I don’t have the heart to think again.
    • Also incidentally beg a wave of answers, thought for a long time or did not understand, thank you first……..