【interview】Microsoft面经
~~收集的面经~~
1. 实现hashtable的put 和get操作
参考:https://yikun.github.io/2015/04/01/Java-HashMap%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86%E5%8F%8A%E5%AE%9E%E7%8E%B0/
put:
- 对key的hashCode()做hash,然后再计算index;
- 如果没碰撞直接放到bucket里;
- 如果碰撞了,以链表的形式存在buckets后;
- 如果碰撞导致链表过长(大于等于TREEIFY_THRESHOLD),就把链表转换成红黑树;
- 如果节点已经存在就替换old value(保证key的唯一性)
- 如果bucket满了(超过load factor*current capacity),就要resize
get:
- bucket里的第一个节点,直接命中;
- 如果有冲突,则通过key.equals(k)去查找对应的entry
若为树,则在树中通过key.equals(k)查找,O(logn);
若为链表,则在链表中通过key.equals(k)查找,O(n)
2. 给定一个8*8的棋盘,一个起始位置si,sj, 一个终止位置ei,ej,求问马从起始位置到终止位置最少需要多少步。
附:八皇后问题
https://blog.csdn.net/friendbkf/article/details/49892039
https://www.cnblogs.com/xinghuan/p/6061824.html
3. 给定一棵二叉树,求这颗二叉树最大子路径和,包括横跨根结点的路径
https://blog.csdn.net/feeltouch/article/details/78511214
public class Solution { private int max = Integer.MIN_VALUE; public int maxPathSum(TreeNode root) { helper(root); return max; } public int helper(TreeNode root) { if(root == null) return 0; int left = helper(root.left); int right = helper(root.right); //连接父节点的最大路径是一、二、四这三种情况的最大值 int currSum = Math.max(Math.max(left + root.val, right + root.val), root.val); //当前节点的最大路径是一、二、三、四这四种情况的最大值 int currMax = Math.max(currSum, left + right + root.val); //用当前最大来更新全局最大 max = Math.max(currMax, max); return currSum; } }
4. 每k个反转单链表。
https://blog.csdn.net/beiyetengqing/article/details/7596707
public static Node reverse (Node head, int k) { Node current = head; Node next = null; Node prev = null; int count = 0; /*reverse first k nodes of the linked list */ while (current != null && count < k) { next = current.next; current.next = prev; prev = current; current = next; count++; } /* next is now a pointer to (k+1)th node Recursively call for the list starting from current. And make rest of the list as next of first node */ if(next != null) { head.next = reverse(next, k); } /* prev is new head of the input list */ return prev; }
Struct btree{
Int value;
Btree*l;
Btree*r;
}
int maxn=-1;
int find_max(Btree*root){
if(!root)
return 0;
int l = find_max(root->l)
int r = find_max(root->r)
int m = max(i,j)+root->value;
int m2
if(m>maxn)
maxn=m;
return m;
}