Remember an interview experience

In an interview about 5 or 6 days ago, I wrote down the questions at that time. Later, there was a competition from Tencent in the past few days, but I didn't organize it. As a result, the competition left me stunned for a while, and my brain cells were all overwhelmed. After death, I will sort out the interview questions at night and restore my brain cells. . This interview let me know how salty my fish is, and how extensive and profound C++ is. .

Question one:

Why does .C++ define a virtual destructor and how is polymorphism implemented? (This question is actually quite basic, and it must be asked in C++ interviews)

Answer: C++ polymorphism is divided into compile-time polymorphism and run-time polymorphism. Compile-time polymorphism refers to function overloading and generic programming, and run-time polymorphism refers to virtual functions.

Mainly talk about virtual functions, adding virtual in front of the function is a virtual function. Add the virtual keyword before the function of the base class, rewrite the function in the derived class, and the runtime will call the corresponding function according to the actual type of the object. If the object type is a derived class, the function of the derived class is called; if the object type is the base class, the function of the base class is called. (There is a saying that is very good, but I forgot to read it.) If virtual is not added, the object will call the function according to the declared type, and if virtual is added, the object will call the function according to the actual type.

(It is recommended to read the book C++ Disassembly Demystification for the underlying implementation of C++ virtual functions.) When a virtual function is defined in a class, the compiler will save all virtual function addresses in the class in a table, which is called virtual function address. table (virtual table). At the same time, the compiler will also add a hidden data member to the class, which becomes a virtual table pointer. This pointer holds the first address of the virtual table and is used to record and find virtual functions. As long as there is a virtual function in the class, there must be a virtual table, and if there is subclass inheritance, even if there is no virtual function in the subclass, the subclass will have a virtual table. If a subclass inherits multiple parent classes, and multiple parent classes have their own virtual functions, then there will be an array of virtual table pointers in the subclass, including the virtual table pointers of each parent class.

 Question two:

 

Give an experience of dealing with C++ memory (leak) that you have encountered

Answer: No. . . . (It's embarrassing because I don't know what to ask, and I really don't have the experience..)

But then I searched the Internet and found a very detailed and complete post (unfortunately I only found it now) https://www.cnblogs.com/findumars/p/5929831.html?utm_source=itdadao&utm_medium=referral

After reading it, I found that it is actually some problems with the pointer. . .

 Question three:

Benefits of C++ Smart Pointers

Answer: (Wow!! It’s scary, I only know generic programming for this thing, and I don’t know anything else at all... Of course, I searched the Internet for a wave)

https://blog.csdn.net/xt_xiaotian/article/details/5714477 very detailed

https://blog.csdn.net/zy19940906/article/details/50470087 Analysis from the bottom

After reading this, I found the interviewer's routine, I asked the second question and found that I didn't know, so I asked the smart pointer... just to hang me...

Question four:

Talk about the data structure list

A: This question can actually be discussed for a long time, and the interviewer also gave it an opportunity, but unfortunately I didn't grasp it. At that time, I only talked to the interviewer about linear lists, linked lists, doubly linked lists, and circular linked lists, and only talked about the concepts, but did not go into depth.

Linear table: The memory is continuous and stored in an array. The advantages are easy to query, and the disadvantages are difficult to insert and delete.

Linked list: The memory is not continuous, and nodes are used to store information. The advantages are simple to insert and delete, but difficult to query. There can be a head node, and a head node can store the length of the linked list in the head node without traversing the entire array each time.

Doubly linked list: Each node not only has a pointer to the next node, but also contains a pointer to the previous node. The previous node of the head node is empty, and the next node of the tail node is empty. The advantage over a singly linked list is that the previous node of the current node can be found in O(1).

Circular linked list: On the basis of the doubly linked list, the head node adds a pointer to the tail node, and the tail node also adds a pointer to the head node, which is more convenient to query the node information in the linked list.

This problem can also be extended to the STL list. Let’s talk about the vector through the STL list, and then compare the two. To sum up, this problem can be discussed in about 10 minutes. Unfortunately, I only talked about it in 5 or 6 minutes. . .

Question five:

Talk about a sorting algorithm with a time complexity of nlog(n)

Answer: The key to quick sort is to find a benchmark, and then rank the numbers less than the benchmark in front of the benchmark, and the numbers greater than the benchmark in the back. Then sort the numbers on both sides of the benchmark in the same way, and this process is carried out recursively.

Quick sort method, this problem is the easiest for me (at that time, I said a two-point, two-point is a search, not a sorting..)

Question six:

Low-level implementation of MFC message map

A: I don't know the MFC message mapping, but I know the Windows message mechanism. Then I talked nonsense to the interviewer and listened to the interviewer's reaction. Then the interviewer asked me to talk about the process of registering a new window again, and he talked nonsense again. Finally, he asked the meaning of the parameter lpfnWndProc , and then I said it was the window processing message function. . . .

Question seven:

Algorithm question: Given a very long string (unknown characters), how to find the first non-repeating character in it (and output its position)

Answer: The first idea is to write an array, and then store all uppercase and lowercase letters. For example, arr[3] means arr[x - 'a'], x is the letter encountered, and then when a letter is encountered, the corresponding letter Increment the array element by one. As a result, the interviewer said that if you don't know what this character is, and you don't know how long the string is. Emmmmmm.... I'm so confused about this, there are so many characters. . . Then no answer was given within the valid time period either. I haven't figured out this question yet. There are big brothers who will give pointers to the younger brother. If I find the answer, I will post it in the comments.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324912361&siteId=291194637