Microsoft's 22 questions (including answers) on data structure Algorithms)

Source: Internet
Author: User

1. Reverse a linked list. Loop algorithm.


1 List reverse (List l ){
2 if (! L) return l;
3 list cur = l. next;
4 list pre = l;
5 list tmp;
6 pre. next = null;
7 while (cur ){
8 tmp = cur;
9 cur = cur. next;
10 tmp. next = pre
11 pre = tmp;
12}
13 return tmp;
14}

2. Reverse a linked list. Recursive Algorithms.

1 List resverse (list l ){
2 if (! L |! L. next) return l;
3
4 List n = reverse (l. next );
5 l. next. next = l;
6 l. next = null;
7}
8 return n;
9}


3. The breadth first traverses the binary tree.
1 void BST (Tree t ){
2 Queue q = new Queue ();
3 q. enque (t );
4 Tree t = q. deque ();
5 while (t ){
6 System. out. println (t. value );
7 q. enque (t. left );
8 q. enque (t. right );
9 t = q. deque ();
10}
11}
----------------------
1 class Node {
2 Tree t;
3 Node next;
4}
5 class Queue {
6 Node head;
7 Node tail;
8 public void enque (Tree t ){
9 Node n = new Node ();
10 n. t = t;
11 if (! Tail ){
12 tail = head = n;
13} else {
14 tail. next = n;
15 tail = n;
16}
17}
18 public Tree deque (){
19 if (! Head ){
20 return null;
21} else {
22 Node n = head;
23 head = head. next;
24 return n. t;
25}
26}

4. Output all arrays of a string. Note that there are repeated characters.

1 char [] p;
2 void perm (char s [], int I, int n ){
3 int j;
4 char temp;
5 for (j = 0; j <n; ++ j ){
6 if (j! = 0 & s [j] = s [J-1]);
7 elseif (s [j]! = '@'){
8 p [I] = s [j];
9 s [j] = '@';
10 if (I = N-1 ){
11 p [n] = '\ 0 ';
12 printf ("% s", p );
13} else {
14 perm (s, I + 1, n );
15}
16 s [j] = p [I];
17}
18}
19}
--------------------------
1 void main (){
2 char s [N];
3 sort (s );
4 perm (s, 0, strlen (s ));
5}


5. Input a string and output a long integer.

1 long atol (char * str ){
2 char * p = str;
3 long l = 1; m = 0;
4 if (* p = '-'){
5 l =-1;
6 + + p;
7}
8 while (isDigit (* p )){
9 m = m * 10 + p;
10 + + p;
11}
12 if (! P) return m * l;
13 else return error;
14}


6. Determine whether a linked list has a loop.

1 int isLoop (List l ){
2 if (! L) return-1;
3 List s = l. next;
4 while (s & s! = L ){
5 s = s. next;
6}
7 if (! S) return-1;
8 else reutrn 1;
9}
-----------
1int isLoop (List l ){
2 if (! L) return 0;
3 p = l. next;
4 wihle (p! = L & p! = Null ){
5 l. next = l;
6 l = p; p = p. next;
7}
8 if (p = l) return 1;
9 return 0;
10}
In fact, during my interview, I also asked other algorithms that do not break the structure.
My answer is to traverse from the linked list header. If the next pointer of a node points to itself, the loop exists; otherwise, the next Pointer Points to itself and traverses the next node. Until the next pointer is null, there is no loop in the linked list.


7. Reverse a string.

1 void reverse (char * str ){
2 char tmp;
3 int len;
4 len = strlen (str );
5 for (int I = 0; I <len/2; ++ I ){
6 tmp = char [I];
7 str [I] = str [len-I-1];
8 str [len-I-1] = tmp;
9}
10}

8. Implement the strstr function.

1int strstr (char [] str, char [] par ){
2 int I = 0;
3 int j = 0;
4 while (str [I] & str [j]) {
5 if (str [I] = par [j]) {
6 + + I;
7 + + j;
8} else {
9 I = I-j + 1;
10 j = 0;
11}
12}
13 if (! Str [j]) return I-strlen (par );
14 else return-1;
15}

9. Implement the strcmp function.

1int strcmp (char * str1, char * str2 ){
2 while (* str1 & * str2 & * str1 = * str2 ){
3 ++ str1;
4 ++ str2;
5}
6 return * str1-* str2;
7}


10. Calculate the number of digits 1 in an integer.

1 int f (int x ){
2 int n = 0;
3 while (x ){
4 + + n;
5 x & = x-1;
6}
7 return n;
8}


11. Tower of Hanoi problems.


1 void tower (n, x, y, z ){
2 if (n = 1) move (x, z );
3 else {
4 tower (n-1, x, z, y );
5 move (x, z );
6 tower (n-1, y, x, z );
7}
8}


12. Minimum steps of the three-column tower.

1 int f3 (n ){
2 if (f3 [n]) return f3 [n];
3 else {
4 if (n = 1 ){
5 f3 [n] = 1;
6 return 1;
7}
8 f3 [n] = 2 * f3 (n-1) + 1;
9 return f3 [n];
10}
11}

Minimum steps of the Four-column tower.
1int f4 (n ){
2 if (f4 [n] = 0 ){
3 if (n = 1 ){
4 f4 [1] = 1;
5 return 1;
6}
7 min = 2 * f4 (1) + f3 (n-1 );
8 for (int I = 2; I <n; ++ I ){
9 u = 2 * f4 (I) + f3 (n-I );
10 if (u <min) min = u;
11}
12 f4 [n] = min;
13 return min;
14} else return f4 [n];
15}


 13. Delete the elements in another linked list from one linked list.

1 void delete (List m, List n ){
2 if (! M |! N) return;
3 List pre = new List ();
4 pre. next = m;
5 List a = m, B = n, head = pre;
6 while (a & B ){
7 if (a. value <B. value ){
8 a = a. next;
9 pre = pre. next;
10} else if (a. value> B. value ){
11 B = B. next;
12} else {
13 a = a. next;
14 pre. next =;
15}
16}
17 m = head. next;
18}

14. In an array, the subscript ranges from 0 to n, and the element is an integer from 0 to n. Determine whether duplicate elements exist.

1int hasDuplicate (int [] a, int n ){
2 for (int I = 0; I <n; ++ I ){
3 while (a [I]! = I & a [I]! =-1 ){
4 if (a [a [I] =-1) return 1;
5 a [I] = a [a [I];
6 a [a [I] =-1;
7}
8 if (a [I] = I) {a [I] =-1 ;}
9}
10 return 0;
11}


15. Determine whether a binary tree is balanced.

1int isB (Tree t ){
2 if (! T) return 0;
3 int left = isB (t. left );
4 int right = isB (t. right );
5 if (left> = 0 & right> = 0 & left-right <= 1 | left-right> =-1)
6 return (left <right )? (Right + 1): (left + 1 );
7 else return-1;
8}
9


16. returns the depth of a binary tree.

1int depth (Tree t ){
2 if (! T) return 0;
3 else {
4 int a = depth (t. right );
5 int B = depth (t. left );
6 return (a> B )? (A + 1) (B + 1 );
7}
8}


17. Two linked lists, one up and one down. Merged into an ascending linked list.

1 List merge (List a, List d ){
2 List a1 = reverse (d );
3 List p = q = new List ();
4 while (a & a1 ){
5 if (a. value <a1.value ){
6 p. next =;
7 a = a. next;
8} else {
9 p. next = a1;
10 a1 = a1.next;
11}
12 p = p. next;
13}
14 if (a) p. next =;
15 elseif (a1) p. next = a1;
16 return q. next;
17}


 18. convert a long string.

1char * ltoa (long l ){
2 char [N] str;
3 int I = 1, n = 1;
4 while (! (L/I <10) {I * = 10; ++ n}
5 char * str = (char *) malloc (n * sizeof (char ));
6 int j = 0;
7 while (l ){
8 str [j ++] = l/I;
9 l = l % I;
10 I/= 10;
11}
12 return str;
13}
19. Using a Data Structure
1 if (x = 0) y =;
2 else y = B;

1 j [] = {a, B };
2 y = j [x];


20. Delete the specified element from the two-way linked list.

1 void del (List head, List node ){
2 List pre = new List ();
3 pre. next = head;
4 List cur = head;
5 while (cur & cur! = Node ){
6 cur = cur. next;
7 pre = pre. next;
8}
9 if (! Cur) return;
10 List post = cur. next;
11 pre. next = cur. next;
12 post. last = cur. last;
13 return;
14}

21. The elements in the ascending array are output repeatedly.


1 void outputUnique (char [] str, int n ){
2 if (n <= 0) return;
3 elseif (n = 1) putchar (str [0]);
4 else {
5 int I = 0, j = 1;
6 putchar (str [0]);
7 while (j <n ){
8 if (str [j]! = Str [I]) {
9 putchar (str [j]);
10 I = j;
11}
12 + + j;
13}
14}
15}


22. During the interview, I encountered the following questions:

1. How to delete the m-th element of the linked list? My method is to first use the pre pointer to step m from the linked list header, create a pst node, next pointer to the header node, cur pointer to the header node, and then pre, cur, the three pointers of post are stepping together. When pre points to the end of the linked list, cur points to the m-last element. Finally, the pst pointer is used to delete the cur-directed element.

2. How can I determine whether a string is symmetric? For example, a, aa, and aba. Set the head and tail pointers to align to the middle until they meet each other.

3. How can I use function 2 to find all symmetric substrings in a string? Use the substring header pointer and tail pointer as the loop variable to set two nested loops to locate all substrings and apply two functions to each substring.

 

This series of questions by http://www.cnblogs.com/wqguan/archive/2006/06/20/430347.html
By GwQ, I just resend

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.