1、微软面试一百道题目精选第9 题判断整数序列是不是二元查找树的后序遍历结果题目:输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果。如果是返回true,否则返回false。例如输入5、7、6、9、11、10、8,由于这一整数序列是如下树的后序遍历结果:8/ 6 10/ / 5 7 9 11因此返回true。如果输入7、4、6、5,没有哪棵树的后序遍历的结果是这个序列,因此返回false。ANSWER:This is an interesting one. There is a traditional question that requires the binary tree to
2、be re-constructed from mid/post/pre order results. This seems similar. For the problems related to (binary) trees, recursion is the first choice.In this problem, we know in post-order results, the last number should be the root. So we have known the root of the BST is 8 in the example. So we can spl
3、it the array by the root.int isPostorderResult(int a, int n) return helper(a, 0, n-1);int helper(int a, int s, int e) if (e=s) return 1; int i=e-1; while (aeai & i=s) i-; if (!helper(a, i+1, e-1) return 0; int k = l; while (ae=s) i-; return helper(a, s, l);第10 题翻转句子中单词的顺序。题目:输入一个英文句子,翻转句子中单词的顺序,但单词内
4、字符的顺序不变。句子中单词以空格符隔开。为简单起见,标点符号和普通字母一样处理。例如输入“I am a student.”,则输出“student. a am I”。Answer:Already done this. Skipped.第11 题求二叉树中节点的最大距离.如果我们把二叉树看成一个图,父子节点之间的连线看成是双向的,我们姑且定义距离为两节点之间边的个数。写一个程序,求一棵二叉树中相距最远的两个节点之间的距离。ANSWER:This is interesting. Also recursively, the longest distance between two nodes mus
5、t be either from root to one leaf, or between two leafs. For the former case, its the tree height. For the latter case, it should be the sum of the heights of left and right subtrees of the two leaves most least ancestor.The first case is also the sum the heights of subtrees, just the height + 0.int
6、 maxDistance(Node * root) int depth; return helper(root, depth);int helper(Node * root, int &depth) if (root = NULL) depth = 0; return 0; int ld, rd; int maxleft = helper(root-left, ld); int maxright = helper(root-right, rd); depth = max(ld, rd)+1; return max(maxleft, max(maxright, ld+rd);第12 题题目:求1
7、+2+n,要求不能使用乘除法、for、while、if、else、switch、case 等关键字以及条件判断语句(A?B:C)。ANSWER:1+.+n=n*(n+1)/2=(n2+n)/2it is easy to get x/2, so the problem is to get n2though no if/else is allowed, we can easilly go around using short-pass.using macro to make it fancier:#define T(X, Y, i) (Y & (1i) & X+=(Y 1;第13 题:题目:输入一
8、个单向链表,输出该链表中倒数第k 个结点。链表的倒数第0 个结点为链表的尾指针。链表结点定义如下:struct ListNodeint m_nKey;ListNode* m_pNext;Answer:Two ways. 1: record the length of the linked list, then go n-k steps. 2: use two cursors.Time complexities are exactly the same.Node * lastK(Node * head, int k) if (k0) error(“k 0;k-) if (pk-next!=NUL
9、L) pk = pk-next; else return NULL; while (pk-next!=NULL) p=p-next, pk=pk-next; return p;第14 题:题目:输入一个已经按升序排序过的数组和一个数字,在数组中查找两个数,使得它们的和正好是输入的那个数字。要求时间复杂度是O(n)。如果有多对数字的和等于输入的数字,输出任意一对即可。例如输入数组1、2、4、7、11、15 和数字15。由于4+11=15,因此输出4 和11。ANSWER:Use two cursors. One at front and the other at the end. Keep tr
10、ack of the sum by moving the cursors.void find2Number(int a, int n, int dest) int *f = a, *e=a+n-1; int sum = *f + *e; while (sum != dest & f e) if (sum left), &(root-right); mirror(root-left); mirror(root-right);void mirrorIteratively(Node * root) if (root = NULL) return; stack buf; buf.push(root);
11、 while (!stack.empty() Node * n = stack.pop(); swap(&(root-left), &(root-right); if (root-left != NULL) buf.push(root-left); if (root-right != NULL) buf.push(root-right); 第16 题:题目(微软):输入一颗二元树,从上往下按层打印树的每个结点,同一层中按照从左往右的顺序打印。例如输入8/ 6 10/ / 5 7 9 11输出8 6 10 5 7 9 11。ANSWER:The nodes in the levels are p
12、rinted in the similar manner their parents were printed. So it should be an FIFO queue to hold the level. I really dont remember the function name of the stl queue, so I will write it in Java.void printByLevel(Node root) Node sentinel = new Node(); LinkedList q=new LinkedList(); q.addFirst(root); q.
13、addFirst(sentinel); while (!q.isEmpty() Node n = q.removeLast(); if (n=sentinel) System.out.println(“n”); q.addFirst(sentinel); else System.out.println(n); if (n.left() != null) q.addFirst(n.left(); if (n.right()!=null) q.addFirst(n.right(); 第17 题:题目:在一个字符串中找到第一个只出现一次的字符。如输入abaccdeff,则输出b。分析:这道题是200
14、6 年google 的一道笔试题。ANSWER:Again, this depends on what is “char”. Lets assume it as ASCII.char firstSingle(char * str) int a255; memset(a, 0, 255*sizeof(int); char *p=str; while (*p!=0) a*p +; p+; p = str; while (*p!=0) if (a*p = 1) return *p; return 0; / this must the one that occurs exact 1 time.第18
15、题:题目:n 个数字(0,1,n-1)形成一个圆圈,从数字0 开始,每次从这个圆圈中删除第m 个数字(第一个为当前数字本身,第二个为当前数字的下一个数字)。当一个数字删除后,从被删除数字的下一个继续删除第m 个数字。求出在这个圆圈中剩下的最后一个数字。July:我想,这个题目,不少人已经见识过了。ANSWER:Actually, although this is a so traditional problem, I was always to lazy to think about this or even to search for the answer.(What a shame.).
16、Finally, by google I found the elegant solution for it.The keys are:1) if we shift the ids by k, namely, start from k instead of 0, we should add the result by k%n2) after the first round, we start from k+1 ( possibly % n) with n-1 elements, that is equal to an (n-1) problem while start from (k+1)th
17、 element instead of 0, so the answer is (f(n-1, m)+k+1)%n3) k = m-1, so f(n,m)=(f(n-1,m)+m)%n.finally, f(1, m) = 0;Now this is a O(n) solution.int joseph(int n, int m) int fn=0; for (int i=2; i1, _r); multiply(_r, _r, tmp); if (n & 1 = 1) multiply(tmp, A, _r); else memcpy(_r, tmp, 4*sizeof(int); 第20
18、 题:题目:输入一个表示整数的字符串,把该字符串转换成整数并输出。例如输入字符串345,则输出整数345。ANSWER:This question checks how the interviewee is familiar with C/C+? Im so bad at C/C+.int atoi(char * str) int neg = 0; char * p = str; if (*p = -) p+; neg = 1; else if (*p = +) p+; int num = 0; while (*p != 0) if (*p=0 & *p m) findCombination(
19、m, m); int auxn; memset(aux, 0, n*sizeof(int); helper(m, 0, aux);void helper(int dest, int idx, int aux, int n) if (dest = 0) dump(aux, n); if (dest = 0 | idx=n) return; helper(dest, idx+1, aux, n); auxidx = 1; helper(dest-idx-1, idx+1, aux, n); auxidx = 0;void dump(int aux, int n) for (int i=0; in;
20、 i+) if (auxi) printf(“%3d”, i+1); printf(“n”);PS: this is not an elegant implementation, however, it is not necessary to use gray code or other techniques for such a problem, right?第22 题:有4 张红色的牌和4 张蓝色的牌,主持人先拿任意两张,再分别在A、B、C 三人额头上贴任意两张牌,A、B、C 三人都可以看见其余两人额头上的牌,看完后让他们猜自己额头上是什么颜色的牌,A 说不知道,B 说不知道,C 说不知道
21、,然后A 说知道了。请教如何推理,A 是怎么知道的。如果用程序,又怎么实现呢?ANSWERI dont like brain teaser. As an AI problem, it seems impossible to write the solution in 20 min.It seems that a brute-force edge cutting strategy could do. Enumerate all possibilities, then for each guy delete the permutation that could be reduced if fail
22、ed (for A, B, C at 1st round), Then there should be only one or one group of choices left.But who uses this as an interview question?第23 题:用最简单,最快速的方法计算出下面这个圆形是否和正方形相交。3D 坐标系原点(0.0,0.0,0.0)圆形:半径r = 3.0圆心o = (*.*, 0.0, *.*)正方形:4 个角坐标;1:(*.*, 0.0, *.*)2:(*.*, 0.0, *.*)3:(*.*, 0.0, *.*)4:(*.*, 0.0, *.*
23、)ANSWERCrap. I totally cannot understand this problem. Does the *.* represent any possible number?第24 题:链表操作,(1).单链表就地逆置,(2)合并链表ANSWERReversing a linked list. Already done.What do you mean by merge? Are the original lists sorted and need to be kept sorted? If not, are there any special requirements?I will only do the sorted merging.Node * merge(Node * h1, Node * h2)
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1