Freshly released PHP advanced interview questions are here! [Answer attached]

藏色散人
Release: 2023-04-09 10:28:01
forward
9325 people have browsed it

Actually, I'm still working recently, but I'm in the process of resigning, so I posted my resume but didn't submit it. When I saw the invitations, I interviewed with a few that were highly matched with me, and got several offers one after another. Recall a wave of interview questions. I divided them according to categories, because some of them also forgot which interview they were in. The questions listed were all impressive questions, and some of them forgot. If the answer is wrong, please correct me. (More:PHP interview questions summary)

<strong>mysql</strong>

1 .Talk about what points you need to pay attention to when writing SQL statements?

Answer:

1. Select * Question: Give the client whatever it needs, and don’t give extra fields. This situation may also cause the statement that could have covered the index to not be able to Go to covering index.
2. Do not perform function operations on query statement fields, as this will invalidate the index.
3. Be sure to avoid mysql automatic type conversion, such as where ‘9’ =9.
4. If possible, try not to set fields that allow null, because null will cause mysql to make an extra layer of judgment.
5. When using like, if the wildcard character % is at the front, a full table scan will also be performed.
Welcome to add.

2. You have been talking about indexing just now, tell me some of the skills you know about indexing

Answer:

1. Focus on fields with high distinction. Create an index, otherwise the index is meaningless.
2. When building a string index, pay attention to the size. If the index length is too long, it will take up more space. If appropriate, it can be intercepted and indexed. The disadvantage is that the covering index cannot be used. Arrange it appropriately according to the business.
3. When establishing a joint index, you must know the leftmost prefix principle. For example, (name, email, phone). In the end, the joint index will only be (name), (name, email), (name, email, phone), others can only access the entire table, and the order of joint indexes needs to be set appropriately based on the business.

3. What is the underlying data structure of the index?

Answer: B-tree.

4. Why are B-trees used and not red-black trees or others?

Answer: You can use red-black trees. However, this may cause the height of the tree to be too high, which means that for the same query, more disk I/O will be performed, affecting performance. However, the B-tree can keep the height of the tree from being too high. The answer to this question is not very good, it is more than that, welcome to add.

5. Do you know index pushdown?

Essentially, it is an optimization for ordinary indexes that need to be returned to the table. That is, during the process of traversing the index pointer, the engine layer first makes some priority judgments and filters out those that do not meet the conditions. Can reduce disk IO.

6. Suppose someone is operating the database and accidentally executes the wrong statement and deletes a lot of data by mistake. Can it be restored at this time? How to recover.

Answer: First of all, bin-log must be turned on. If it is not turned on, it may not be possible to recover. It depends on whether the specific file system can be restored. When bin-log is enabled, the type setting must be set to row or mixed, and statement cannot be set. Then, if a row is deleted by mistake, the corresponding deletion event can be replaced by an insertion event and executed on the standby database. If the table is deleted by mistake, you can first obtain the most recent full backup, put it in the standby database, and then take out the bin-log. Except for the events that are not deleted, other events will be replayed in sequence.

7. Why can’t it be set to statement?

Answer: Set to statement. The actual bin-log stores SQL statements (non-specifically deleted primary key ids). In this case, if it is a master-slave architecture, the master and slave may not select the appropriate index. The same results in master-slave inconsistency.

8. You just talked about master-slave, then please talk about the mechanism of master-slave operation

Answer: First of all, the main library still needs to open the bin-log, and the slave The library first sets the main library to be connected, change master... and then executes start slave. At this time, the slave library will create two threads, one io_thread, which is mainly responsible for connecting to the main database. A sql_thread is mainly responsible for executing relay log statements. First, the main library receives the synchronization request from the slave library, and sends the binary file to the slave library according to the passed bin-log file name and the location where synchronization starts. The slave library io_thread is responsible for putting the received data into the transit log, and then sql_thread is responsible for Read and parse the execution from the transfer log. When the execution is completed, the synchronized position flag is updated.

9. Do you know the master-slave delay? Sometimes the delay can be quite long. What should you do if you encounter this situation?

Answer: Please pay attention to this kind of question. Make key points. When a problem arises and you are looking for a solution, you must prescribe the right solution. In other words, you can think about this problem in this way, under what circumstances the master-slave delay is caused.
1. If the master database and slave database server configurations are different, and the slave database is different, the delay time may be lengthened. At this time, just change to the same server configuration server.
2. The pressure on the slave library is too great. Generally, the master-slave system is used, and the slave library is basically used for query. For example, the operator or the developer may perform a series of SQL operations on the slave library. That's easy. Allocate several more slave libraries to share the pressure, one master and multiple slaves.
3. Big affairs. For example, delete statements should not be limited by limit. If the amount of data is too large, it will take a long time for the main library to run and then synchronize to the slave library. This time interval is too long.

<strong>Design Patterns</strong>

Do you know which design patterns do you usually use? Can you describe it based on your business scenario?

Answer: Here I will first give an example of using Laravel in daily life, which uses a lot of design patterns, such as facade, combination, decoration, observer... I will bring in the specific scenarios, and then talk based on the previous business scenarios. Let’s talk..., and finally I said that design patterns are not silver bullets. Only by using the right pattern in the right scenario can its value be reflected.

<strong>Handwritten algorithm</strong>

Given a sorted array and a specified value, return the specified value The index position in the array, or if it does not exist, returns the index position after inserting the given value into the array. Pay attention to the time complexity.

For example, given the ordered array [1,3,5,6] and the given value 5. Then return the subscript 2.
Given the ordered array [1,3,5, 6] Given a value of 7, return subscript 4.

Answer:

    function searchInsert($nums, $target) {
        if (!count($nums))  return 0;
        $l=0;
        $r = count($nums)-1;
        while ($l <= $r){
            $middle = $l + (($r - $l) >> 1);
            if ($nums[$middle] == $target) return $middle;
            if ($nums[$middle] < $target){
                $l = $middle+1;
            }else{
                $r = $middle-1;
            }    
        }
        return $l;
    }
Copy after login

Typically, bisection can be used, and the time complexity is O(log2n). Space complexity O(1).

<strong>Network</strong>

1. What are the main protocols at the transport layer?

Answer: There are mainly TCP and UDP protocols. The difference between them is that TCP requires a three-way handshake to connect and can ensure the reliability of the message. UDP does not require a connection and does not guarantee message reliability.

2. Can you talk about TCP’s three-way handshake in general?

Answer: First, the server listens to a certain port, and the client initiates a request to carry a syn data packet (for the first time). The server receives this data packet and returns the syn/ack data packet to the client. (the second time), and finally the client sends an ack packet again (the third time).

4. Why is a three-way handshake required?

Answer: Mainly to confirm whether both parties are receiving normally.
First time: The client cannot confirm anything. The server can confirm that the client's sending and receiving are normal, and its own receiving is normal
The second time: The client can confirm that its own sending and receiving are normal, and that the server's sending and receiving are normal. The server can confirm that it is receiving normally and that the client is sending normally.
The third time: Everything can be confirmed.

Concurrency

Assuming that there are multiple entrances that can operate with one account at the same time, this account only For ten yuan, what are some ways to avoid overdeducting consumption? For open questions, as long as it can solve the problem, it is a good solution. There is no single answer.

Answer: mysql: You can directly use where amount>=current_amount and amount>0..., or pessimistic lock for update. redis:lua script. At the PHP level, you can use file locks and queue features. There is only one consumption outlet.

<strong>Design</strong>

If our company has many projects with login functions, how should we design them?

Answer: This login needs to be developed separately as a module, similar to the login center. All other subsystem logins need to be authenticated from this system.

<strong>Others</strong>

1. See if you mentioned using swoole in your project, also write something about go , can you talk about the difference in their coroutines?

Answer: They are the same in design. The main difference is the coroutine scheduler mode. Swoole's coroutine scheduler is single-threaded, and go's coroutine scheduler is multi-threaded. This means that swoole has only one coroutine running at the same time, while go can have multiple coroutines running at the same time. Therefore, there is no need to lock global variables in the swoole coroutine. Moreover, swoole is essentially single-threaded and multi-process, which means that it has no super-global variables, only process-level variables. As for go multi-threading, multi-threading will inevitably have the problem of critical variable locks. Of course, go also provides sync read-write locks out of the box, or you can use channels directly instead.

2. Can you talk about go’s gmp scheduling model?

Answer: Balabala For a long time, I felt that I didn’t explain it clearly. Well, I don’t understand it very well. At this time, I guess in the interviewer's mind, if he had just said he didn't know, it would have been over.

3. Tell me about the most difficult part of your project and how did you solve it?

It depends on your own mastery of the project and the gold content of the project.

The above is the detailed content of Freshly released PHP advanced interview questions are here! [Answer attached]. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template