Why am I against the pure algorithm face question (reprint)

Source: Internet
Author: User

Transferred from: http://coolshell.cn/articles/8138.html

Algorithm interview may be Microsoft out of the interview method, now a lot of companies are emulating, and our programmers are happy to solve algorithmic problems, I personally think, this is the cancer of exam-oriented education! I said in the "How Do I recruit programmers" in the more conservative, " questionings algorithm problem is not wrong, many of the interviewers are just superficial or even wrong understanding of the purpose of the interview algorithm problem ." Today, I would like to reinforce this view- I object to the pure algorithmic interview ! (Note that I'm talking about a purely algorithmic problem)

Once again, I quote one of my previous views-

Solving an algorithmic problem doesn't mean that the person is capable of solving the problem at work, and you can think that the primary math problem may be more difficult than these problems, but it doesn't mean that those arcane experts can solve practical problems.

Well, let's take a look at an example (this is a discussion on Weibo yesterday)--" find the 2nd largest number in an unordered array ", almost everyone uses O (n) algorithm, and I believe that for those of us who are educated out of the exam, we don't have to sort with O (n) Algorithm is very normal thing, even I can not help thinking that O (n) algorithm is the answer to this question. we are too accustomed to the standard answer, which is the saddest place in our education . (The generalized brainwashing is to make your consciousness dependent on a standard answer and then control you by giving you a standard answer that you don't think about)

Analysis of functional requirements

Imagine what we would do if we got such a problem in our actual work. I will analyze this demand, because I am afraid that the future of demand will change, today you asked me to find a 2nd-largest number, tomorrow you find me to find a 4th-largest number, the day after yesterday asked me to find a 100th large number, I do not kill. Change in demand is a normal thing. After analyzing this requirement, I will naturally write the algorithm for the K-large number--the difficulty increases at a sudden.

Many people will think that the need to find the K-Big is a "premature expansion" of the idea, not so, I believe we have written in the actual code too many such programs, you will not design such a function interface--find2ndmaxnum (int* array, int len), As if you would not design the Destroybaghdad (); Such an interface, but design a destorycity (city&); interface, and Baghdad as a parameter to pass in! So, you should be declaring a named Findkthmaxnum (int* array, int len, int kth), passing 2 as arguments. This is the most basic programming method, in mathematical terms, called algebra ! The simplest method of demand analysis is to translate the requirements into function names, and then look at the interface is not very two?!

(Note: Do not dwell on findmaxnum () or findminnum (), because the business meaning of these two function names is very clear, unlike Find2ndmaxnum () two)

Analysis of non-functional requirements

Performance and the like are always non-functional requirements, for algorithmic problems, we like to study the algorithm problem space and time complexity. We want to achieve a double harvest of space and time, which is the style of algorithmic academia. So, accustomed to the standard answer we have lost the ability to think, will only mechanically think about the performance of the algorithm, but ignore the performance outside the algorithm .

If the title is-"Find the K-max number from an unordered array", then we will definitely think about using the O (n) linear algorithm to find out the number of K. In fact, there are linear algorithms--stl can use Nth_element to obtain a similar nth number, which uses the idea of fast ordering, from the array s to randomly find an element x, the array is divided into two parts SA and SB. Elements in the SA are greater than or equal to X,SB elements less than x. In this case there are two cases: 1) The number of elements in SA is less than K, then the first k-| of SB The sa| element is the K-large number, 2) the number of elements in the SA is greater than or equal to K, then the number of K in the SA is returned. The time complexity is approximately o (n).

Academic nuts to this step will surely cheer victory! But where can they get the demand for performance analysis is also sourced from the business!

we say that performance, is basically an individual will ask, how much is the amount of requests? If our Findkthmaxnum () request volume is M times, then your this time to O (n) complexity of the algorithm to get the effect is O (n*m), this is the nerd-style college people never think of. because the exam-oriented education let us not think from the actual.

The solution of engineering type

According to the above requirements analysis, the solution for people with software engineering experience usually follows:

1) sort the array from large to small.

2) So you want the number of k large, directly visit array[k].

Sorting takes only one time, O (N*log (n)), and then the next m-Time call to Findkthmaxnum () is all O (1), but the overall complexity is linear.

In fact, the above is not the best method of engineering, because, in the business, the array of data may be changed, so, if it is sorted by the array, there are data changes will let me reorder, this is too much performance, if there will be a lot of insertions or deletions, then consider using B + tree.

The engineering solution has the following characteristics:

1) It is easy to expand, because the data is well sequenced, you can also easily support a variety of needs, such as from K1 to K2 large data (those college-written code in the need to get this demand and began to scratching instead focusing)

2) structured data simplifies the overall algorithm complexity, resulting in better overall performance. (Public desire for good, its prerequisite)

3) The code becomes clear, easy to understand, easy to maintain! (The Academy's and STL's approximate O (n) complexity algorithm no one dares to move)

Debate

You may have the following arguments with me,

    • If the programmer does this algorithmic problem in a sort of way, he will certainly not be as much as you think . Yes, you're right. But I want to say that many times, we think intuitively, is precisely the right way. Because the idea of "sequencing" is in line with the way the human brain handles problems, the way the school is used is anti-brain intuition. Anti-brain intuition usually means obscure and maintenance costs rise.
    • is an interview problem, I just want to test your algorithmic skills, this is too much . No problem, but we need to know who we're recruiting. Is it a person who can only write algorithms, or a person who can do software? It's only you who know it best.
    • This algorithm is too easy to induce the idea of the College school . Yes this "find the number K", in fact, can be transformed into a more business of the topic-" I want to bid with other merchants, I would like to rank in all competitors quoted in the K, please write a program, I input k, and a product name, the system tells me how much price?" (Quotation of all goods of merchant in an array) "--business analysis, overall performance, algorithms, data structures, increased demand for candidates to reconstruct, this question is all tested.
    • Are you saying that the algorithm is not important, do not learn? do not understand me like this, make it seem that if the interview is not face, I can not learn. algorithm is very important, algorithm problem can exercise our thinking, but also have a lot of practical use . My article is not to let everyone do not learn the algorithm, this is completely wrong, I am let everyone with business problems to use the algorithm. Ask your business questions, and you'll be asked the algorithm questions.

Why am I against the pure algorithm face question (reprint)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.