Byte's interview, how many can you handle?

Which one is faster, C++, Python?

Reader's answer: I don't know where to say this, but in the case of C++, it can actually provide developers with a lot of permissions, that is to say, it can involve some operations at the operating system level, and the speed should be quite fast. Then Python implements the function quite quickly.

Replenish:

Generally speaking, C++ is faster because it is a compiled language that can be directly compiled into machine code and does not require the intervention of an interpreter during execution, so the execution efficiency is higher.

Python is an interpreted language that needs to be executed by converting the code into machine code through an interpreter at runtime, so compared to C++, its execution efficiency is low. But Python has many excellent libraries and frameworks, which can help developers quickly develop efficient applications, thereby improving development efficiency.

Have you ever understood compiled language and interpreted language?

Reader's answer: It may be that C++ provides more machine language, that is, it only needs to perform related compilation and search. But Python may be more like a scripting language, so it needs to be executed during execution. Well, on the one hand, sentence syntax processing, so the execution speed will be slower.

What is the difference between heap memory and stack memory

Reader Answer: The heap in Go mainly allocates some relatively large objects, while the stack may allocate some temporary objects or relatively small data variables. Then if the allocation is, for example, if you apply for a channel, or a very large object, then it will be allocated from the default space on the stack to the heap, because the empty heap space on the heap will be allocated After allocation, it can be retained for a longer time, which is probably a difference between the stack and the heap.

Replenish:

1. Different management methods: stack memory is automatically allocated and released by the system, while heap memory needs to be manually allocated and released by programmers.

2. Different allocation methods: stack memory is a continuous memory space. The system will automatically allocate a certain size of stack space for each thread. Function parameters and local variables will allocate memory on the stack. The heap memory is a discontinuous memory space, and programmers need to obtain heap memory space by dynamically allocating memory.

3. Different size restrictions: The size of stack memory is fixed and much smaller than heap memory. When the program is running, the stack space of each thread is usually only a few MB to tens of MB, and the size of the heap memory depends on the remaining memory size of the system and the dynamic allocation of the programmer.

4. The access speed is different: the access speed of stack memory is much faster than that of heap memory, because the stack memory is continuous, and the offset of the stack pointer can be directly read when accessing local variables and function parameters. The heap memory is discontinuous and the access speed is slow.

Are local variables placed on the heap or on the stack?

Reader's answer: For local variables, because they only exist in one function for the relevant life cycle, they should be on the stack.

What is the difference between linked list and array?

Reader's answer: The length of the linked list is not fixed, and it is very fast to perform related insertion or deletion operations on it. Lookup needs to traverse the value from beginning to end, which is inefficient.

In terms of arrays, in terms of arrays, a fixed-length continuous memory space is pre-allocated, and the subscript index of the array is used to find and assign values. But if you want to perform insertion and deletion operations, you may need to shift all the arrays after the position you want to insert in the data to perform related insertion operations, so the insertion and deletion operations will be Compared with the linked list, it will be troublesome.

Replenish:

It can be mentioned that because the memory addresses of the array are continuous, the CPU cache hit rate can be increased, while the memory addresses of the linked list are not continuous, and the CPU cache hit rate will be very low.

How to dynamically expand the array?

Reader Answer: For go, it actually provides a structure like a slice, that is to say, it maintains a pointer to an array at the bottom layer, and then maintains the length of an array and the capacity value of a CAP pre-stored in its space. Then if the size of the current array space is less than 1024, it is basically a 2-fold expansion, and if it exceeds the size of 1.2 times, it is basically a 1.25-fold expansion.

What is the difference between coroutines and our ordinary threads?

Reader Answer: Coroutines can be understood as user-level threads, so they are more convenient and simpler than processes in terms of size and scheduling.

Replenish:

1. Different scheduling methods: threads are scheduled by the operating system, while coroutines are controlled by programmers. When a thread is scheduled, it will be suspended by the operating system, waiting for the next scheduling. The coroutine is actively called by the programmer in the code, and can switch between different tasks without waiting for the scheduling of the operating system.

2. Different system resource occupation: thread is an entity managed by the operating system, which occupies relatively large system resources, including memory, thread stack, CPU time slice, etc. The coroutine is implemented in user space and does not require the support of the operating system, so it occupies less resources.

3. The switching cost is different: thread switching needs to save and restore the thread context, which takes a certain amount of time and resources. The switching of coroutines only needs to save and restore a small amount of data such as stack frames, so the switching cost is lower than that of threads.

4. The programming model is different: threads are oriented to the operating system, while coroutines are task-oriented. Threads need to use the API provided by the operating system for inter-thread communication and synchronization, while coroutines can use language-level coroutine libraries to achieve cooperative multitasking.

What are the communication methods of coroutines?

Reader's answer: I don't know, when go is used, it may be used in conjunction with channel, creating an endless loop, listening to different semaphores for processing. Or is it referring to the GMP model?

Replenish:

1. Shared memory: Coroutines exchange data through shared memory. This method is simple and direct, but synchronization and mutual exclusion issues need to be considered, otherwise problems such as data competition will occur.

2. Message passing: Coroutines pass data through message queues and other methods. This method can avoid problems such as data competition, but issues such as the order of sending and receiving messages need to be considered.

3. Semaphores: Coroutines use semaphores to achieve synchronization and mutual exclusion. This method needs to consider the number and order of semaphores, otherwise problems such as deadlock will occur.

What issues should be paid attention to in conventional multi-threaded development?

Reader's answer: Scenario considerations, can your thread be created without limit? And creating a thread requires overhead, so the thread pool is generally used to pre-create some allocated thread resources, and then use it first if necessary, and then the thread pool is also responsible Some GC processing.

How many threads should be opened in the thread pool, and the number of threads will be related to your task. CPU-intensive The number of threads is considered according to the number of CPU cores or the time related to task execution.

What are the problems with reading and writing shared variables?

Reader Answer: Concurrent Access

What is deadlock?

Reader's answer: They are waiting for some resources between two or more processes, and then there is no way to completely release the resources they have already occupied, but the resources they need are occupied by other processes. You have no way to preempt, resulting in a circular waiting situation (request hold, mutual exclusion, inalienable, circular waiting)

How to avoid deadlock, how to troubleshoot?

Reader Answer: Add locks or channels, interrupt points, and add log information.

Replenish:

Avoid deadlocks:

Acquire locks in a fixed order: Acquire locks in a fixed order to avoid deadlocks. For example, if thread A first acquires lock 1 and then lock 2, then thread B should first acquire lock 2 and then lock 1.

Set the timeout period: In the process of acquiring the lock, you can set the timeout period. If the lock is not acquired after a certain period of time, the lock acquisition will be abandoned to avoid deadlock caused by waiting for the lock.

Check method:

Use tools: You can use some tools to help detect and locate deadlock problems, such as jstack and jconsole.

Analyzing logs: You can analyze system logs and thread logs to see if any threads are waiting for a certain lock, so as to find out the possible cause of deadlock.

Code inspection: You can check whether there are multiple threads competing for the same lock in the code, and whether there are problems such as lock nesting, so as to find out the cause that may lead to deadlock.

Cryptography and Networking

What encryption algorithms have been used?

Reader Answers:

An encryption algorithm such as Sha256 or MD5. It cannot be restored to the original data and is used to compare whether the data is consistent.

Symmetric and asymmetric encryption algorithm RSA

Replenish:

1. Symmetric encryption algorithms: such as DES, 3DES, AES, etc., use the same secret key to encrypt and decrypt data. The encryption and decryption speed is fast, but the secret key management is difficult.

2. Asymmetric encryption algorithms: such as RSA, ECC, etc., use the public key to encrypt data, and the private key to decrypt data, which has high security, but the encryption and decryption speed is slow.

3. Hash algorithm: such as MD5, SHA-1, SHA-256, etc., which map data of any length into a fixed-length hash value, which is irreversible and tamper-proof, and is mainly used for data integrity verification.

4. Message authentication code (MAC): such as HMAC, CMAC, etc., mix messages and secret keys to generate a fixed-length authentication code to prevent data from being tampered with.

5. Digital signature algorithm: such as RSA, DSA, etc., the message and private key are mixed and processed to generate a digital signature, which is used to verify the source and integrity of the message.

The difference between symmetric and asymmetric encryption algorithms, specifically asymmetric

Reader's answer: In terms of symmetry, it has a public key and a private key, and the private key is only held by you. In this case, other people will not actually get your CL, and then you pass a credible The third party to carry out the relevant wonderful encryption. In this way, others only need to use your public key to obtain your public key to encrypt and decrypt your transmitted content, and then to ensure the security of such a data.

What is the difference between HTTP and HTTPS?

Reader's answer: Because HTTP is transmitted in clear text, then HTTPS adds some encryption protection of SSL on this basis to ensure the security of its transmitted messages.

Replenish:

HTTP is a hypertext transfer protocol, and information is transmitted in clear text, which poses security risks. HTTPS solves the defect of HTTP insecurity, adding SSL/TLS security protocol between TCP and HTTP network layers, so that messages can be encrypted for transmission.

HTTP connection establishment is relatively simple, and HTTP message transmission can be carried out after TCP three-way handshake. However, after the TCP three-way handshake, HTTPS needs to carry out the SSL/TLS handshake process before entering encrypted message transmission.

The default ports of the two are different, the default port number of HTTP is 80, and the default port number of HTTPS is 443.

The HTTPS protocol needs to apply for a digital certificate from a CA (Certificate Authority) to ensure that the identity of the server is trusted.

How to use these encryption algorithms?

Reader's answer: Mixed use of symmetric and asymmetric encryption algorithms, the session key is symmetric, and the process of obtaining it is asymmetric.

Replenish:

The confidentiality of information can be guaranteed through hybrid encryption , which solves the risk of eavesdropping.

hybrid encryption

HTTPS uses a "hybrid encryption" method that combines symmetric encryption and asymmetric encryption :

Asymmetric encryption is used to exchange "session keys" before communication is established , and asymmetric encryption is no longer used in the future.

During the communication process, the plaintext data is encrypted by using the symmetric encrypted "session key".

Reasons for adopting the "hybrid encryption" method:

Symmetric encryption uses only one key, and the operation speed is fast. The key must be kept secret, and secure key exchange cannot be achieved.

Asymmetric encryption uses two keys: a public key and a private key. The public key can be distributed arbitrarily while the private key is kept secret, which solves the key exchange problem but is slow.

What is the difference between TCP and UDP?

Reader's answer: TCP provides a series of reliable transmission mechanisms to ensure that its transmission is reliable. In comparison, its transmission speed is slow, while UDP does not have this reliable control. It just does its best, so its transmission speed is fast, and it takes up less resources. The specific use depends on the different business scenarios for related use.

How to transform UDP into reliable transmission?

Reader Answer: The application layer adds the serial number and ACK. Then the local will cache some messages that you have already sent, and then ask the other party to return ACK after receiving the message to confirm that the message has been received. If you do not receive the ACK, you may set up some timed retransmissions. way to ensure that the message can be successfully sent to the other party.

Replenish:

It is also necessary to implement a sliding window at the application layer to implement flow control. Data is sent according to the receiving capacity of the receiver. There is also congestion control. When a large number of data packets are transmitted in the network at the same time, it will cause network congestion and affect the quality of data transmission. and efficiency. TCP congestion control avoids the occurrence of network congestion by dynamically adjusting the data transmission rate, thereby ensuring the reliability and efficiency of data transmission.

database

Common performance optimization methods of MySQL

Reader's answer: index and sub-database sub-table

After adding the index, what is the difference between the insertion speed and the reading speed?

Reader's answer: The b + tree index structure is actually more consistent with the actual disk structure, so that random writes become related sequential writes, and the insertion speed becomes faster. All data nodes and data information are stored in leaf nodes, which can improve the speed of range search.

What role will Redis play in the architecture?

Reader Answer: When caching is used

It can be directly stored in the process memory, why do you want to specialize in Redis now?

Reader's answer: Because Redis is fast (the interviewer said no, because there is still process communication, it is not as fast as direct memory; to implement a cache by yourself is not necessarily better than Redis, and Redis can do synchronization in a distributed architecture)

 

Guess you like

Origin blog.csdn.net/JACK_SUJAVA/article/details/130268584