Difficult to top~ Didi also asked too many details in the interview....

A few days ago, my junior brother shared his face-to-face scriptures with me after he landed on Didi.

Today I would like to share his Didi Mian Jing. The content of the questions is very detailed, and some of the questions are also very classic. The interview mainly examines subjects such as Go, computer network, Linux, and algorithms.

Golang

1. When did you start contacting Go?

Answer : Butterfly

2. Have you ever used the go interface, what are its features, and what is the application scenario?

Answer : Yes, an interface is a type, which is an abstract collection composed of a set of method signatures. The interface defines the behavior that the object should have, regardless of the specific implementation of the object.

Ah Xiu added

Features : Implementing an interface requires providing a set of method signatures that must be identical to those defined in the interface. If an object implements an interface, then the object must provide the implementation of all the methods in the interface.

Scenario : It is widely used in various fields, such as network programming, concurrent programming, testing, etc. for example:

  1. In network programming, interfaces can be used to define the communication methods of various network protocols.

  2. In concurrent programming, interfaces can be used to define interfaces for various concurrency modes.

  3. In testing, interfaces can be used to define the interfaces of various testing frameworks.

3. Have you ever used go defer, what are its features, and what is the application scenario?

Answer : defer is a keyword in the Go language, which is used to register delayed calls. These calls are not executed until just before return.

Therefore, it can be used for resource cleanup. If there are multiple defer statements, they are executed in a first-in, last-out manner.

Ah Xiu added

defer has the following characteristics:

  1. Delayed calls: defer can register delayed calls, which are not executed until return.

  2. Resource cleanup: defer can be used to release resources, such as closing files, unlocking, etc.

  3. Can be used multiple times: defer can be used multiple times, and each registered deferred call will be executed sequentially in the order of registration.

  4. Can be used with goroutines: defer can also be used with goroutines, when defer is registered in a goroutine, it will be executed when the goroutine exits.

Application Scenario

  1. Concurrency synchronization control: In concurrent programming, defer can be used to ensure that a function is executed after all child coroutines exit.

  2. Lock scenario: In the lock scenario, you can use defer to ensure that the lock is released and prevent problems such as deadlock.

4. The difference between go slices and arrays

Answer : In the Go language, both arrays and slices are used to store a set of data of the same type.

There are two main differences between them:

  1. Arrays are of fixed length whereas slices are of variable length.

  2. Arrays are value types, while slices are reference types.

Specifically, each element in the array has a fixed memory address, and the slice is implemented by the array, which just encapsulates the original array.

Therefore, when using slices, we can change the contents of the original array by modifying the slice.

Ah Xiu added

Since slices are reference types, passing a slice in a function call creates a new copy of the slice, whereas passing an array passes a pointer or reference.

5. Slicing expansion mechanism

A provides a structure like a slice. The bottom layer maintains a pointer to an array, and then maintains the length of an array and a capacity value of a CAP pre-stored in its space.

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.

Netizens added

If go 1.18+, when the oldcap of the original slice is less than 256, the newcap of the new slice is twice the oldcap;

When the capacity of oldcap is greater than or equal to 256, newcap will have a calculation formula: newcap += (newcap + 3*threshold) / 4 and then make a memory alignment for newcap, which is related to the memory allocation strategy.

After memory alignment, the capacity of the new slice must be greater than or equal to the newcap generated according to the first half.

6. How does go manage memory

Answer : Golang's memory management is based on tcmalloc, so it can be said that the starting point is quite high.

The core design idea of ​​the memory allocator of Go language is: multi-level memory allocation module, which reduces the use of locks and system calls during memory allocation; multi-scale memory unit, reduces the fragmentation of memory allocation.

Ah Xiu added

Golang also made a lot of optimizations when it was implemented. If you are interested, you can also take a look at Golang's memory management implementation through the source code.

Golang's memory management implementation mainly involves the following aspects:

  1. Memory allocator (malloc) and releaser (free): Golang uses tcmalloc as its default memory allocator, which is an efficient memory allocator that can reduce memory fragmentation. In the Go language, you can use the built-in functions malloc and free to allocate and free memory.

  2. Garbage collection mechanism: Golang uses Concurrent Mark Sweep (CMS) as its default garbage collection mechanism. CMS is an efficient garbage collection algorithm that can perform garbage collection without blocking user threads.

  3. Memory pool technology: Golang uses memory pool technology to improve the efficiency of memory allocation and release. Memory pool is a technology that pre-allocates a certain amount of memory, which can avoid frequent calls to system functions to allocate and release memory, thereby improving program performance.

  4. Large object support: Golang supports large objects, i.e. objects larger than 1MB. To support large objects, Golang uses a data structure called "variable size array", which can dynamically adjust the size of the array at runtime.

Golang's memory management implementation is very efficient and flexible, and can meet the needs of various scenarios.

7. How to know whether the variable is in the heap area or the stack area

Answer : In Golang, how variables are assigned depends on the scope of the variable.

If the variable is defined inside the function, it will be allocated on the stack; if the variable is defined outside the function, then it will be allocated on the heap.

If possible, the Golang compiler will allocate the local variables of the function to the function stack frame (stack frame).

However, if the compiler cannot ensure that the variable is no longer referenced after the function return, the compiler will allocate the variable on the heap.

Also, if a local variable is very large, it should also be allocated on the heap instead of the stack.

8. The difference between threads and coroutines in go

Answer : In Go, threads and coroutines are both ways of concurrent execution.

Threads are resources allocated by the operating system, while coroutines are created manually by programmers. The memory space of the process is shared between threads, and the stack space is shared between coroutines.

Switching between threads is faster than switching between coroutines because they require more context switching. However, since the memory space of the process is shared among threads, problems such as data races and deadlocks may occur.

In contrast, switching between coroutines is faster because they share their own stack space. This makes coroutines easier to manage and also avoids some concurrency issues.

9. Have you ever used go coroutines? What is the application scenario?

Answer : Coroutines are concurrent execution methods in the Go language, which are manually created by programmers. Coroutines share their own stack space, so some common concurrency problems, such as deadlocks and data races, can be avoided.

Ah Xiu added

The application scenarios of Go coroutines include but not limited to:

  • Asynchronous I/O operations

  • network programming

  • background task processing

  • concurrent crawler

10. How do you often use coroutines? Write some code

computer network

1. The difference between a router and a switch

A: Not a good answer, I just said something casually.

Ah Xiu added

Functionally speaking:

Both routers and switches are devices in a computer network, but they function differently.

A router is a device that connects local area networks and wide area networks in the Internet, and is used for inter-network connections, that is, for connecting different networks.

The switch is a device for expanding the network, which can provide more connection ports in the sub-network so as to connect more computers.

Hierarchically speaking:

Ordinary switches generally work at the second layer of the OSI seven-layer model, the data link layer, while routers work at the third layer, the network layer.

2. When the browser enters a url, what will happen specifically, which protocols are used in it, and which layer these protocols are in

Answer : Classic questions, omitted.

Ah Xiu added

1. First check the browser cache to see if there is any cached one, if not

2. Check the local host file,

3. Call API, Socket function gethostbyname under Linux

4. Send a DNS request to the DNS server and query the local DNS server, which uses the UDP protocol

5. If the ARP address resolution protocol is used to perform ARP query in a subnet, if it is not in a subnet, it needs to perform DNS query on the default gateway. If it cannot be found, it will always search for the root DNS server until it finally gets the IP address. (More than 400 root DNS servers worldwide, managed by 13 different organizations)

6. At this time, we have the IP address of the server and the default port number. HTTP is 80 by default and https is 443 port number. Yes, first try http and then call Socket to establish a TCP connection.

7. After the connection is successfully established after three handshakes, start to transmit data. If it is the http protocol, just return and finish.

8. If it is not the http protocol, the server will return a redirection message starting with 5, telling us that we are using https, which means that the IP has not changed, but the port number has changed from 80 to 443. Okay, let’s wave again four times , done,

9. Let’s do it again. This time, in addition to changing the above-mentioned port number from 80 to 443, SSL encryption technology will also be used to ensure the security of the transmitted data and ensure that the data will not be modified or replaced during the transmission process.

10. This time, it is still a three-way handshake, communicate the authentication algorithm, encryption and verification algorithm used by both parties, and also verify the CA security certificate of the other party during the process.

11. After confirming that it is correct, start communication, and then the server will return some data of the website you want to visit. During this process, the interface will be rendered, involving ajax technology and the like, until finally we see the colorful webpage

3. The difference between https and http

Answer : The data transmitted by the HTTP protocol is unencrypted, that is, in plain text, so it is very unsafe to use the HTTP protocol to transmit private information. The HTTPS protocol is a network protocol built by the SSL+HTTP protocol that can perform encrypted transmission and identity authentication. It is safer than the http protocol.

Ah Xiu added

1. The https protocol needs to go to CA to apply for a certificate. Generally, there are few free certificates, so a certain fee is required.

2. http and https use completely different connection methods and different ports. The former is 80 and the latter is 443.

Linux

1. Have you ever used Linux, and what operations are commonly used?

Answer : chmod chown useradd groupadd netstat etc.

Ah Xiu added

File related (mv mkdir cd ls)

docker-related(docker container ls docker ps -a )

Test-related (test connectivity: ping test port connectivity: telnet)

2. How to check the port used by the process?

Answer : netstat

Ah Xiu added

You can view the ports used by a process with the following command:

netstat -tunlp | grep <进程ID>

Among them, -t means TCP connection, -u means UDP connection, -n means no DNS resolution, -l means only display the port in listening state, -p means display process ID and name.

3. How to check the current cpu status?

Answer : top command

Algorithms & the rest

1. Algorithm: Find the intersection node of two linked lists

2. Algorithm: Give a nested Json string as input and convert it into a linear structure array

3. Introduction of project torture and thesis

4. Has Kafka been used, how to use it, and what are the characteristics of the Kafka consumer group?

Guess you like

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