This article is reproduced fromCoolshell.cn
Algorithm interview may be an interview method developed by Microsoft. Now many companies are following suit, and our programmers are willing to solve algorithm questions. I personally think this is a cancer of exam-oriented education! I said conservatively in "How do I recruit programmers?","There is no mistake in asking difficult questions about algorithms. Many of the people who are wrong are simply superficial or even mistaken in understanding the purpose of the interview algorithm questions..", Today, I want to strengthen this point of view --I am opposed to algorithm-only interview! (Note: I am talking about pure algorithm issues)
I again quoted one of my previous ideas --
The ability to solve algorithm problems does not mean that this person has the ability to solve problems at work. You can think about it. Primary school mathematical questions may be more difficult than these questions, but it does not mean that the Oracle experts can solve practical problems.
Let's take a look at an example (this example is a discussion on Weibo yesterday).Find the 2nd largest unordered Array", Almost all people use the O (n) algorithm. I believe that it is normal for us to use the O (n) Algorithm without sorting, even I cannot help myself think that the O (n) algorithm is the standard answer to this question.We are too accustomed to the standard answer. This is the saddest part of our education.. (Brainwashing in a broad sense is to make your consciousness dependent on a standard answer, and then give you a standard answer so that you will not control you by thinking about it)
Functional Requirement Analysis
Imagine what we would do if we had such a question in our actual work? I will definitely analyze this demand, because I am afraid that the demand will change in the future. Today, you ask me to find a 2nd big number, and tomorrow you will find a 4th big number, the day after tomorrow, I asked me to find a 100th big number. Demand changes are normal. After analyzing this requirement, I will naturally write the algorithm for finding the k-th largest number-the difficulty increases at once.
Many people think that the demand for K is a kind of idea of "premature expansion". This is not the case. I believe we have written too many such programs in actual code, you won't design such a function interface -- find2ndmaxnum (int * array, int Len), as if you won't design an interface like destroybaghdad, instead, we designed a destorycity (City &); interface, and passed Baghdad as a parameter! Therefore, you should declare a findkthmaxnum (int * array, int Len, int KTH) and pass 2 as a parameter.This is the most basic programming method. In mathematics, it is called algebra.! The simplest method of requirement analysis is to translate the requirement into a function name. Then, let's see if this interface is not very good ?!
(Note: Do not tangle with findmaxnum () or findminnum (), because the business meaning of these two function names is very clear, unlike find2ndmaxnum)
Non-functional requirement analysis
Performance and other things are never non-functional requirements. For algorithm questions, we like to study the space and time complexity of algorithm questions. We hope to achieve a good harvest of space and time, which is the style of algorithm academia. So,We have lost the ability to think about the standard answer. We only mechanically think about the performance within the algorithm, and ignore the performance beyond the algorithm..
If the question is: "finding the maximum number of K from the unordered array", we will definitely think about using the linear algorithm of O (n) to find the maximum number of K. In fact, there is also a linear algorithm-STL can use nth_element to obtain a number similar to the n-th largest. It uses the fast sorting idea to randomly find an element x from array S, the array is divided into two parts: SA and Sb. The element in SA is greater than or equal to X, and that in Sb is smaller than X. In this case, there are two situations: 1) if the number of elements in SA is smaller than K, then the K-| sa | element in Sb is the k-th large number; 2) if the number of elements in SA is greater than or equal to K, the maximum K number in SA is returned. The time complexity is approximately O (n ).
The academic nuts will surely win this step! However, where do they want to get the performance requirement analysis is also from the business!
When we talk about performance, I am usually asked, What is the Request volume? If our findkthmaxnum () Request volume is m times, the effect of your algorithm that requires O (n) Complexity every time is O (n * m ), this is never imagined by a nerdy academic school.Because exam-oriented education does not allow us to think from reality.
Engineering Solution
According to the above requirement analysis, the solutions of people with software engineering experience are usually as follows:
1) sort the array from large to small.
2) If you want to increase the number K, you can directly access array [K].
Sort only once, O (N * log (n), then, the next M calls to findkthmaxnum () are all O (1, the overall complexity is linear.
In fact, the above is not the best solution for engineering, because in the business, the data in the array may change. Therefore, if the array is used for sorting, data changes will make me re-sort. This is too performance-consuming. If there are many insert or delete operations in actual situations, you can consider using the B + tree.
Engineering solutions have the following features:
1) It is very convenient to expand, because the data is sorted, you can also easily support various needs, for example, from the first K1 to the second K2 data (the code written by those schools began to scratch their heads when they got this requirement)
2) regular data simplifies the overall algorithm complexity and improves the overall performance. (For good deeds, you must first sharpen your tools)
3) The Code becomes clear, easy to understand, and easy to maintain! (No one dared to change the academic algorithm with the same O (n) complexity as STL)
Debate
You may have the following arguments with me,
- If a programmer sorts this algorithm question, he will not think as much as you think.. Yes, you are right. But I want to say that, in many cases, our intuitive thinking is exactly the right path. Because the idea of "sorting" is in line with the way the human brain handles problems, the academic approach is anti-brain intuition. Anti-brain intuition usually means obscure and increases maintenance costs.
- I just want to test your algorithm skills. This is too much.. No problem, but we need to know who we are recruiting? Is it a person who can only write algorithms or a person who can make software? You are the only one who knows this.
- This algorithm question is too easy to be induced by academic ideas.. Yes, this "finding the k Number" can actually be transformed into a more business question-"I want to bid with other merchants. I want to rank the K name quoted by all competitors. Please write a program. I enter K and A commodity name. The system will tell me how much price to order?(The quote of all commodities of a merchant is in an array.) "-- this is a question that involves business analysis, overall performance, algorithms, data structures, and increased demands for restructuring candidates.
- Are you talking about algorithms that are not important?Don't understand me like this. It seems that if I don't have an interview, I can leave school.Algorithms are very important. algorithm questions can train our thinking and have a lot of practical use.. This article is not intended to prevent you from learning algorithms. It is totally wrong. I want you to use algorithms with business problems. When you ask your business questions, you will also be asked about algorithms.
Summary
After reading the above analysis, I believe you understand why I am opposed to purely algorithmic interview questions. The reason isAn algorithm-only interview question cannot reflect the overall quality of a program.!
In the interview, what comprehensive qualities should we consider for programmers? I thought there were the following:
- Will it perform requirement analysis? How to understand the problem?
- What is the solution? What is the idea?
- Will the basic algorithms and data structures be flexibly used?
In addition, we know that for software development, the following challenges are difficult to implement in Engineering:
- The maintenance cost of software is much higher than the development cost of software.
- The quality of software has become more and more important, so testing has become more and more important.
- The demand for software is always changing, and the demand for software is always increasing at 1.1 points.
- A large amount of code in the program is processing incorrect or abnormal processes.
Therefore, for programming capabilities, we should mainly consider the following capabilities of programmers:
- Whether the design meets the requirements and can respond to potential demand changes.
- Is the program easy to read and maintain?
- What is the ability to refactor code?
- Will you test your own program?
Therefore, during this period of time, I am more and more inclined to ask candidates some questions with business significance, and should increase or change the demand to view the programmer's ability to refactor the code. After writing the program, design test cases for candidates.
For example: parse addition, subtraction, multiplication, division expressions, convert strings to numbers, shuffles programs, password generators, find locations through IP addresses, two-way English-Chinese dictionary retrieval ......
In short, I am opposed to purely algorithmic interview questions