How to answer algorithm questions during the interview

Source: Internet
Author: User

The question of purely algorithm testing during the interview is generally hated by many programmers. Here I will share some of my ideas and skills on solving algorithm questions.

Generally, the articles on algorithms start with classical algorithms. This is an introduction to an algorithm. When there are many algorithms, you will naturally have an idea, however, it takes too much time and effort to learn and is not suitable for blog communication. This article focuses on quick thinking. Many people are almost blank in their minds when facing algorithm questions. This article is about starting from the question, several common skills for opening a question without any idea.

(1) simplified to traditional

As a matter of fact, it is indeed difficult to get the correct idea in the first place for many problems. At this time, we can try a simple and traditional way of thinking. First, we need to narrow down the problem scope to a very easy solution.

[Question] Is there a sufficient amount of 2, 5, and 1 cent coins? How many methods are there to get one yuan together?

At first glance, this question seems to be totally impossible to start with. However, based on the simplified and traditional ideas, we can first consider extreme and simple situations. If we narrow down the problem: there are enough 1 cent coins. How many methods can I collect 1 cent of money? No doubt, the answer is 1.

After getting this answer, We Can slightly expand the scope of the question: how many methods can there be enough 1 cent coins and 2 cent coins? How many methods can I collect n cents? The answer is still 1.

Next, we can expand the problem from another perspective. How many methods are there to get one cent coin and two cent coin together? At this time we have the hand of a sufficient amount of 1 cent coin, together with any more money only one way, then only 1 cent together with N-2 cent, there is a way, only 1 cent together n-4 cent money, there is 1 method, only 1 cent together n-6 cent money, there is 1 kind of method ......

And together these N-2, n-4, n-6 these money, each of them to make up 2 cent, will produce a new way to gather together n cent of money, the total number of these methods + 1, it is the number of methods to collect n cents using 1 cent coin and 2 cent coin.

It is a very beneficial attempt to immediately adopt this approach during the interview. Solving small-scale problems can make you more familiar with the problem and slowly discover the characteristics of the problem, the most important thing is to give your interviewer a positive signal-It looks much better to analyze the problem right away than to frown.

For this question, we can quickly find there are two dimensions of the problem: planting coins with a a1-ak and gathering together n cent, so we can remember P (k, n ). When we find the recursive formula P (k, n) = P (K-1, n-ak) + P (K-1, n-2 * ak) + P (K-1, n-3 * ak )...... this problem has been solved.

Generally, it is very effective to solve the dynamic planning problem from a simple to traditional approach. When a certain amount of simple problem solutions are accumulated, the answer to a higher level of questions is always at hand.

 

(2) split into two parts

Another way of thinking is to split the problem into two parts, which is the same as the original problem. If we divide the problem into two parts, we can divide it into four parts, it can be further divided into 8 until it is divided into problems that are easy to solve. When trying this kind of thinking, we only need to consider two questions: 1. Is the problem simplified after being split into two parts? 2. Based on the solution of the two questions divided into two parts, can the solution of the entire problem be easily obtained?

[Question] sorts an array.

This classic algorithm is certainly not familiar to everyone. However, if you think about this from the beginning, not everyone can come up with one of several classic sorting algorithms, here is just an example to illustrate the application of the split idea.

The simplest way is to divide the array into two halves and sort them separately. For two ordered arrays, we have a way to combine them into an ordered array. Therefore, this idea of splitting is feasible. For arrays already divided into two halves, we can also split this array into two halves until we have only one element in the array, and the array of one element is naturally ordered. It is not hard to see that we come up with the "merge sort" in the classic array sorting algorithm ".

Another way of thinking is to split the array into two halves. Considering that it is complicated to split the array into two halves, we can consider dividing the array into two halves Based on the element greater than or less than a certain number, in this way, we can directly connect to an ordered array as long as we solve them separately. This problem can also be split into two parts again. According to this idea, we can get the "quick sorting" in the classic array sorting algorithm ".

 

(3) Virtual Reality

This idea is aimed at the special problems related to floating-point numbers, because the computing problems related to floating-point numbers (especially the computational ry) are difficult to enable, So virtual reality is used, it refers to replacing a floating point with an integer that is somewhat "virtual. The specific method is to sort the floating point numbers (not limited to floating point numbers, we don't care about their specific integers) in the question, and then use the serial number of the floating point number instead of itself to think about the problem, it will be replaced after calculation.

[Question] We know the horizontal and vertical rectangles of n edges (represented by [x1, y1, x2, y2]), and calculate the total area covered by them.

Because the coordinates may contain floating-point numbers, this question seems very complicated (you can practice the previous ideas from simplicity to complexity and the idea of splitting them into two are basically ineffective, the covering relationship of a rectangle is only related to the size of the rectangle coordinate, so we try to sort all the x values of the rectangle, and then use the serial number instead of the specific vertical, and the y value is also true, so we get that all the rectangles are actually in a 2nx2n block, so that we can use the simplest method to calculate whether each 1x1 grid is overwritten. So far, as long as we convert the real length and width of the grid back when calculating the area, we can get the answer to the question.

 

This article is intended to be written when I discuss algorithm issues during the interview in the QQ group one day. The above three ideas are the fast direction of thinking when I encounter algorithm problems. They are not a panacea, if it cannot take effect, you need to calm down and slowly think and observe. Considering that there are basically no difficult algorithm problems during the interview, the hit rate of these skills should not be too low and shared with everyone, hope to help.

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.