A foreigner (n) from my team encountered a good problem today. I asked if I would like to have a simulated interview. Although I am also an intern, N has rich experience in interviews, and I am also visiting others. So I made a decisive statement, sure. The problem is as follows (translated into Chinese for easy viewing ):
There is an integer array, a sub-array SA of this array, to find a sub-array that meets the following conditions
1. Sub-array contains SA
2. The sum of the sub-array is 0.
3. Small Sub-array
4. The sub-array must be continuous.
For example, if the array is {1,-1, 2, 8,-9, 4, 0}, SA is {8,-9}, that is, the subscript of the array is 3 (including) to the part where the subscript is 5 (not included) (the subscript starts from 0 ). Then the child array is {-1, 2, 8,-9 }.
>_< Before looking down, you can imagine what to do if you are an interviewer. The idea part is about 10 min >_<
My policy at that time was to first give me a simple and crude method and then change it slowly. The simple method is to enumerate every sequence that contains the sub-sequence. The complexity is O (n ^ 2 ). When the result is improved to the O (nlogn) method, the change fails. In desperation, I had to give up improving this method. Then I thought of enumerating from the left of the initial subsequence, enumerating the right of the subsequence, and finally calculating the intersection between the left and right sides. On the left or right side of the enumeration, only O (n) is required, but O (N ^ 2) is required for calculation ). It is still unsuccessful to reduce the complexity by using a method similar to sub-governance. Ask n later whether the subsequence meeting the condition exists. N the answer is that it must exist or not. For convenience, I choose to assume that it must exist and be unique. Therefore, we propose to remove the initial subsequence (hypothesis and sum) and find the continuous subsequence with the sum in the remaining array. The results were not optimized until the time was reached.
Enter the coding stage. N let me implement the algorithm that first enumerates the left and right sides and then computes the Left and Right intersections. A few minutes later, N looked at it and said "good. The interview is over.
Finally, I asked N for some feedback, which is summarized as follows:
A. At the beginning, confirm each condition with the interviewer. For example, does this sequence exist? Must the designed algorithm be a deterministic algorithm? Is a sub-sequence smaller? Is it a minimum sub-sequence? This is very important, because sometimes the interviewer will deliberately blur certain conditions, and then expect the interviewer to ask clearly; sometimes some conditions can direct you to think of a new idea, for example, can an algorithm be a random algorithm.
B. In the idea stage, check whether the subject's thinking is active. It is best to give the best idea. If you don't give it, you need to give as many methods as possible. The complexity of these algorithms can be the same, but the idea needs to be different. For example, the above sequence enumeration and the first enumeration of the left and right sides, and then the calculation intersection can be considered as two idea. In other words, if the optimization is not sure, the best practice is to first give several algorithms with the same complexity but different idea, and then select one for improvement. This allows you to maintain a good rating when you think of the best algorithm. As I did above, it would be quite a loss.
C. In the coding stage, we mainly observe whether the interviewer can successfully perform a simple coding, that is, there are almost no bugs when writing. This can only rely on the usual efforts. Then, during coding, note that you are currently in the interview. Do not set the variable name or function name too long. However, the best practice is to ask the interviewer what kind of code to view.