Simple Algorithm questions for Microsoft interviews)

Source: Internet
Author: User
Simple Algorithm questions during Microsoft interviews (for example)

(Note: These questions are not a pattern. They are about your basic knowledge. Anyone who is smart and has no practical knowledge will be eliminated from these questions .)
1. What is the difference between a linked list and an array?

ANSWER mainly understands the basic concepts. However, it is better to consider a comprehensive aspect. Now, the company's recruitment competition may come in detail. Whoever is more careful, the chances of winning will be great.

1) arrays are stored one by one in the memory, that is, if One If the element is in address A, the second element of the array is in address A + 1. The linked list is not, and each node in the linked list has no relatively fixed location relationship. A node after address A is not necessarily A + 1, but is in A random state in other idle areas of the memory.

2) once an array is explicitly declared, its size is fixed and cannot be expanded dynamically. A linked list can dynamically generate nodes and add them to the end of an existing linked list.

3 )...... (Let's think about it)

 

2. Compile an algorithm for sorting linked lists. Why do you choose to use this method?

The ANSWER linked list is usually inserted for sorting. Why? When sorting is inserted into an array, a large amount of data is moved to delete elements with incorrect positions. This is inefficient in deleting ordered tables. From a mathematical point of view, the delete operation for an ordered table (that is, an array) is O (n ). the linked list is different. Because its storage location is not fixed, it only takes O (1) Time to delete elements at a fixed location, so the overall performance is greatly improved.

 

3. Compile an algorithm for sorting arrays. Why do you choose to use this method?

The ANSWER sorting algorithm is very mature. In fact, sorting is a very effective example of the algorithm. When answering the questions, try to find some more technical algorithms, such as heap sorting or quick sorting. If you write bubble or something, others will write, which will not show your excellence. Of course, pay attention to the given conditions. If you don't want to sort the three numbers, you can make a quick ranking. This is a bit of a "Cool-killing.

 

4. Compile the code that can directly implement the strstr () function.

ANSWER first needs to know what the strstr () function is for. Check the C language books by yourself. Generally, the C language standard library will be provided at the end of the appendix. This topic is actually an important algorithm category, called "string pattern matching ". It has many ready-made algorithms, the simplest of which is simple matching algorithms, and advanced algorithms such as KMP and BM. It is too late to write written test estimates. The following is a simple matching algorithm.

Int stringMatching (char * pattern, char * text)

{

Int pLen = strlen (pattern), tLen = strlen (text );

For (int I = 0; I <= tLen-pLen; I ++ ){

For (int j = 0; pattern [j] = text [I + j]; j ++ );

If (j = pLen) return I;

}

Return-1; // Not found

}

 

 

5. Write a program to reverse the string and optimize the speed and space.

ANSWER: loop is certainly the simplest.

Void reverseString (char * str)

{

Int n = strlen (str );

For (int I = 0; I <n/2; I ++)

{Int t = str [I]; str [I] = str [n-I-1]; str [n-I-1] = t ;}

}

 

6. How do I find circular links in a linked list?

ANSWER: Obviously, you only need to determine whether there is a backtracing pointer. Determine whether a node has a pointer pointing to its previous position. In actual implementation, we can imitate the access flag array method in DFS. We can Design An access flag of the node, which is set to visited. Each time a node is accessed, the visited domain is set to 1. In this way, if the visited domain of a subsequent node is already 1, you can determine that it has a circular link. The specific code is too simple.

 

7. Write a function and check whether the character is an integer. If yes, return its integer. (Or: How to Write a function from string to long integer using only four lines of code ?)

Analysis: simple! Scan once, and the highest bit of the corresponding integer is generated each time. One line is done!

Long convert (char * s_string, long s_integer)

{

For (int sLen = strlen (s_string), I = 0; I <sLen; s_integer + = (s_string [I ++]-'0') * pow (10, sLen-I-1 ));

Return s_integer;

}

 

8. A function is provided to output all the arrays of a string.

ANSWER supports Simple backtracking. Of course, there are also many algorithms for the generation of permutation. Let's look at the combination of mathematics, as well as reverse order generation and arrangement, and some methods that do not require Recursive Generation and arrangement. The first volume of Knuth's <TAOCP> gave a deep dive into the generation of the arrangement. Understanding these algorithms requires a certain degree of mathematical skills and inspiration. If you are interested, you 'd better take a look.

Void permStr (char * str, int I)

{

If (I = strlen (str)-1)

Printf ("% s" n ", str );

Else

{

For (int j = I; j <strlen (str); j ++)

{

Swap (& str [I], & str [j]);

PermStr (str, I + 1 );

Swap (& str [I], & str [j]);

}

}

}

 

9. A function is provided to copy two strings A and B. The last few bytes of string A overlap with the first few bytes of string B.

AnSwer remembers that this type of question is often your consideration of boundaries. In addition to technical proficiency, careful programming is also very important. In fact, many programming masters may not have special technologies, but are often very patient and careful. Remember: programming is the most basic job in computer science, it is the easiest way to master, be patient, and be careful. The Code is as follows:

Char * myStrcpy (char * s, char * a, char * B, char n)

{

Int aLen = strlen (a), bLen = strlen (B );

If (n> aLen | n> bLen)

Return NULL; // Error

For (int I = 0; I <aLen + bLen-n; I ++)

If (I <aLen-n) s [I] = a [I];

Else s [I] = B [I-aLen + n];

S [I] = '"0 ';

Return s;

}

 

10. How to write a program and put an ordered integer array into a binary tree?

ANSWER: Method for building binary search trees. A simple recursive structure. I really don't understand. I just wrote it down. The algorithm design of the tree must be associated with recursion, because the tree itself is the definition of recursion. Recursion here should be taken for granted. However, learning to rename recursion as non-recursion is also a necessary technique. After all, recursion will cause stack overflow. It is best not to use the program at the bottom of the system. However, for certain mathematical questions, we must learn to use recursion. Solution .

Void insertNode (bTree ** root, int val)

{

BTree * newNode = (bTree *) malloc (sizeof (bTree ));

NewNode-> data = val;

NewNode-> lChild = NULL;

NewNode-> rChild = NULL;

If (! (* Root ))

* Root = newNode;

Else if (newNode-> data <(* root)-> data)

InsertNode (& (* root)-> lChild, val );

Else

InsertNode (& (* root)-> rChild, val );

}

 

11. How do I print Binary Tree node data layer by layer from the top? Programming.

There is nothing to say about ANSWER Binary Tree hierarchy traversal. If you don't want to review the basics earlier. After learning it, you will find that the most important thing is the most basic and simple.

Typedef struct myBinaryTree

{

Int data;

Struct myBinaryTree * lChild;

Struct myBinaryTree * rChild;

} BTree;

 

Struct myQueen

{

BTree * que [QSIZE];

Int front;

Int rear;

} BinQueue; // Global var

 

Void initQueue ()

{

// Front = real makes the queue empty

BinQueue. rear = QSIZE-1;

BinQueue. front = binQueue. rear;

For (int I = 0; I <QSIZE; I ++)

BinQueue. que [I] = NULL;

}

 

Int enQueue (bTree * newNode)

{

If (binQueue. front> = 1)

BinQueue. que [binQueue. front --] = newNode;

Else return 0;

Return 1;

}

 

BTree * deQueue ()

{

Int t;

If (binQueue. front! = BinQueue. rear ){

T = binQueue. rear;

BinQueue. rear --;

Return binQueue. que [t];

}

Else return NULL;

}

Int levelTraversal (bTree ** root)

{

InitQueue ();

BTree * lc = (bTree *) malloc (sizeof (bTree ));

BTree * rc = (bTree *) malloc (sizeof (bTree ));

BTree * p = (bTree *) malloc (sizeof (bTree ));

If ((! Lc) | (! Rc) | (! P )){

Printf ("OVERFLOW" n ");

Exit (OVERFLOW); // Allocation Error

}

P = * root;

If (! P ){

Printf ("Empty Tree, build it first! "N ");

Return 0;

}

EnQueue (p); // enqueue the root of the tree

While (binQueue. front! = BinQueue. rear ){

P = deQueue ();

Printf ("% d", p-> data );

Lc = p-> lChild;

Rc = p-> rChild;

If (lc! = NULL)

EnQueue (lc );

If (rc! = NULL)

EnQueue (rc );

}

Printf ("n ");

Return 1;

}

 

12. How to drop a linked list in an order (that is, reverse order, pay attention to the linked list boundary conditions and consider empty linked lists )?

As mentioned earlier, ANSWER is the most basic and important. The linear data structure is Learning You must be familiar with the data structure. Microsoft's questions are still different from those of domestic companies. The first concepts in China are the same as those in history.

Typedef struct listNode

{

Struct listNode * link;

Int data;

} Node;

 

Node * getNode (node * newNode, int val)

{

If (! NewNode)

Exit (OVERFLOW );

NewNode-> link = NULL;

NewNode-> data = val;

Return newNode;

}

/*

Insert a new node after p

*/

Int insertNode (node * prev, node * newNode)

{

If (! Prev) return 0;

NewNode-> link = prev-> link;

Prev-> link = newNode;

Return 1;

}

/*

Delete the node after the node prev

*/

Int eraseNode (node * prev, node * p)

{

If (p = NULL)

Return 0;

Prev-> link = p-> link;

Free (p );

Return 1;

}

Void buildList (node * head)

{

Int value;

Node * newNode = (node *) malloc (sizeof (node ));

Node * p = head;

Scanf ("% d", & value );

While (value! =-1 ){

NewNode = getNode (newNode, value );

InsertNode (p, newNode );

P = p-> link;

NewNode = (node *) malloc (sizeof (node ));

Scanf ("% d", & value );

}

}

 

Int reverseList (node * head)

{

Node * p = head-> link;

Node * q = p-> link;

If (p = NULL ){

Printf ("The list is empty! "N ");

Return 0;

}

While (q! = NULL ){

Node * newNode = (node *) malloc (sizeof (node ));

NewNode = getNode (newNode, q-> data );

InsertNode (head, newNode );

EraseNode (p, q );

Q = (node *) malloc (sizeof (node); // Allocate again

Q = p-> link;

}

P-> link = NULL;

Return 1;

}

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.