Algorithm surface Questions

Source: Internet
Author: User

In the previous blog has the original mention to share a question, recently also can new company into the job not long, busy familiar with the environment, plus not long ago went out to play a trip (by the way wrote a travel, interested can see a look). So there is no time to organize the blog, this time weekend finally idle down, taking advantage of the weekend to record several interviews encountered in the process of algorithmic problems.

This blog does not want to introduce the high-force lattice algorithm (such as Winnow,bagging,ada boost, etc.), to tell the recent interview process encountered in the algorithm and interview time to give the answer (the ratio of the high rate of the algorithm problem I have listed, the others do not say). The algorithm can be said to solve all the problems of the cornerstone. A lot of things can be converted to algorithmic problems, the most important function of learning algorithm is to understand a lot more clearly. It is the so-called, knowing that it is better known. Many people think that the program is the data structure + algorithm + appropriate comments. Do not learn the algorithm, then do not learn programming. Although it may not be so exaggerated, because in fact there are many small projects do not need too many algorithms, is the code kept to the top overlay on the line. But even if it is not so exaggerated, but as a programmer, logical thinking is the idea of the algorithm is still very important. ---this passage is what an interviewer in a company says.

vWrite in front

Again, here is just a list of some of the most recently encountered algorithm interview questions, to give you to share the interview experience (only a common algorithm experience, there are some less common on the lazy list, technical direction of not introduced). And there is my answer. PS: Whether it is algorithmic or the answer is not any representation is not the right answer, just the answer I gave during the interview, just to share. In addition, for the questions I have listed and the answers given, if there is a friend of the park or a better solution, then share it! Write algorithm is a very enjoyable enjoyment!

By the way, because there was an interview interviewer in London, so only remote interview, we are using collabedit, this stuff is still very useful.

vAlgorithmic Encyclopedia

----Algorithm Encyclopedia from Baidu Encyclopedia (PS: Do not pick up the introductory text in this, straight to the point of the topic. It feels a little duangduang. The impatient can jump right over here! )

The algorithm (algorithm) is an accurate and complete description of the solution, a series of clear instructions to solve the problem, and the algorithm represents a systematic method to describe the strategy of solving the problem. In other words, the input of a certain specification can be obtained in a limited time with the required output. If an algorithm is defective, or is not suitable for a problem, executing the algorithm will not solve the problem. Different algorithms may use different time, space, or efficiency to accomplish the same task. The merits and demerits of an algorithm can be measured by the complexity of space and time. The instruction in the algorithm describes a calculation that, when run, starts with an initial state and (possibly null) initial input, passes through a set of finite and clearly defined states, and eventually produces output and stops at a final state. A transition from one state to another is not necessarily deterministic. Some algorithms, including random inputs, are included in the randomization algorithm. The conceptual part of the formal algorithm derives from an attempt to solve the decision problem posed by Hilbert, and later attempts to define an effective computational or effective method for shaping. These attempts included recursive functions proposed by Courtes Godel, Jacques Herbrand and Stephen Core Cleny in 1930, 1934 and 1935 respectively, Alonzo Chow Kit in 1936, the λ calculus, 1936 Emil Leon Post the Turing of the formulation 1 and Alan 1937 presented. Even at the present moment, it is often difficult to define an intuitive idea as a formalized algorithm.

vAlgorithm topics
    • First question: In a sorted int array, look for a number, if there is a number, return to the position of the array, and vice versa return-1 See the Blogger interview answer
    • The second question: {"12,bob", "3,sky", "6,cool", "1,good", "22,go"}, sorted by the first column of the element to see the blogger interview answer
    • Topic Three: string array removal Duplicates view blogger interview answers
    • Question fourth: Co-locate an unordered array of shapes, the negative numbers in the array move to the left of the array, and the positive numbers move to the right of the array. 0 Do not move (PS: I open to do this problem is intended to use a no-brain sort of fix, the result I began to write, the interviewer said "here to hint, the moving array is only negative in the left, positive number in the right." Do not have to use traditional sorting, if not the traditional sort can add points. ") Check the blogger interview answer

All the answers are for reference only, not standard or correct answers, only the answers given during the interview, so please give us a better answer.

All the answers are written by Notepad during the interview, I don't have a double check when I come back, I post the code directly, and some of them are appended with the diagram. If everyone is interested, I also suggest that you can first write to read with Notepad, because the algorithm this thing itself is a train of thought only. There's no need for vs.

vRefer to the first question of answer
//------------------------------------------------------------------------------//<copyright file= "Runner.cs" company= "Cnblogs Corporation" owner= "Please call me brother" >//Copyright (C) 2015-2016 All rights Reserved//Original post address:http://www.cnblogs.com/toutou///</copyright>//------------------------------------------------------------------------------namespacetestapp{usingSystem; usingSystem.Collections.Generic; usingSystem.Text.RegularExpressions; classRunner {Static voidMain (string[] args) {            int[] arr = {3,5,6,7,8,9, -, at, $, About, the, the, the, the, the, the, the, the,98,534,555,676,878,988,1365 }; intNumber =555; intresult =Search (arr, number);            Console.WriteLine (result);        Console.readkey (); }         Public Static intSearch (int[] arr,intNumber ) {            intresult =0; if(arr = =NULL|| Arr. Length = =0|| Number > Arr[arr. Length-1] || Number < arr[0]) {result= -1; }            Else{result= Bisearch (arr. Length-1, arr, number); }            returnresult; }         Public Static intBisearch (intEndIndex,int[] arr,intNumberintStartIndex =0)        {            intresult =0; if((Endindex-startindex) <2)            {                 for(inti = StartIndex; I <= EndIndex; i++)                {                    if(Arr[i] = =Number ) {Result=i;  Break; }                    Else{result= -1; }                }            }            Else            {                if(Arr[startindex] <= number && number <= arr[(EndIndex + startIndex)/2]) {Bisearch (EndIndex+ startIndex)/2, arr, number, startIndex); }                Else{bisearch (EndIndex, arr, number, EndIndex+ startIndex)/2+1); }            }            returnresult; }    }}
On the way back to the interview, I thought about the idea, which is basically this: Of course, I'm not the best solution.

Back to the first question I have a different view

Second question
namespacetestapp{usingSystem; usingSystem.Text.RegularExpressions; classProgram {Static voidMain (string[] args) {            string[] Source =New string[] {"12,bob","3,sky","6,cool","1,good","22,go" };  for(inti =0; I < source. Length; i++)            {                 for(intj = i; J < Source. Length; J + +)                {                    if(Convert.ToInt32 (Regex.match (source[i),@"\d+"). Value) > Convert.ToInt32 (Regex.match (SOURCE[J),@"\d+"). Value)) {stringtemp =Source[i]; Source[i]=Source[j]; SOURCE[J]=temp; }                }            }             for(inti =0; I < source. Length; i++) {Console.WriteLine (source[i]); }        }    }}
The problem I chose is to use the more stupid regular to take out the numbers, and then use the bubble sort of algorithm completed. Later came back to think, really should not ah! Here's a traditional bubble sort of vision.

Back to the second question I have a different view

Third question
namespacetestapp{usingSystem; classProgram {Static voidMain (string[] args) {            string[] Source = {"AAA","BBB","AAA","CCC","BBB","Ddadd","CCC","AAA","BBB","DDD" }; foreach(varItemincharrdistinct (source))            {Console.WriteLine (item); }        }         Public StaticString[] Arrdistinct (string[] source) {            if(Source! =NULL&& source. Length >0) {array.sort (source); intSize =1;  for(inti =1; I < source. Length; i++)                    if(Source[i]! = source[i-1]) Size++; string[] Temparr =New string[size]; intj =0; Temparr[j+ +] = source[0];  for(inti =1; I < source. Length; i++)                     if(Source[i]! = source[i-1]) Temparr[j++] =Source[i]; returnTemparr; }            returnsource; }    }}
// because the main test center in the string array to go heavy, so sort I don't care too much, directly with the system sort. Of course, this is very unprofessional. However, if necessary, you can use the algorithm to sort the strings first, and then use my method. As for the algorithm sort string, I can think of a more "despicable" approach is only the first to convert the string array into char and then into an int array,array.convertall<stringintint . Parse (s)); // reorder, Big God, you got a better idea? Don't hide it, come on! 

Return to the third question I have a different view

Question Fourth
 Public int[] Intarrsort (int[] source) {            if(Source = =NULL|| Source. Length = =0)                returnsource; intRightindex = source. Length-1, Tempnumber =0;  for(inti =0; I < source. Length; i++)            {                if(I >Rightindex) Break; if(Source[i] <=0)                {                    Continue; }                Else if(Source[i] >0)                {                     for(intj = Rightindex; J >=0; j--)                    {                        if(Source[j] <0) {Tempnumber=Source[j]; SOURCE[J]=Source[i]; Source[i]=Tempnumber; Rightindex=J;  Break; }                    }                }            }            returnsource; }

Algorithm surface Questions

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.