题目来源[http://google-interview.com/google-interview/post/2009/08/01/Real-interview-questions.aspx]
1. Please describe a data structure that accomplishes the functions INSERT and FIND EDIAN in the best possible asymptotic time.
两个堆。一个大顶堆,一个小顶堆。满足:1、大顶堆中所有数小于小顶堆中所有数;2、大顶堆中数字的个数大于等于小顶堆中数字的个数,且相差小于等于1。则,查找中位数时,取大顶堆的最大数。复杂度O(1)。Insert时,先根据大小,插入相应的堆中;然后,若大顶堆中数字的个数比小顶堆多2,则从大顶堆中取最大数,放入小顶堆中。复杂度O(lgn)。
2. Given a circularly sorted array, describe the fastest way to locate the largest element.
circularly sorted:5 6 7 1 2 3 4。折半查找.find(left, right),算法复杂度O(lgn)。Cur=(left+right)/2. 若left<cur,则left=cur;否则right=cur。
3. Given a binary search tree and an integer k, describe the most efficient way to locate two nodes of the tree that sum to k.
类似已排序的数组,在其中寻找x+y=k。算法复杂度O(lgn)。
4. Given an arbitrarily connected graph, explain how to ascertain the reachability of a given node.
5. If you have one billion numbers and one hundred computers, what is the best way to locate the median of the numbers?
划分区间,计算每个居间中的数字个数。不断缩小区间。因为有100台计算机,因此每台每次计算10millian个数。
复杂度O(lgn)/100 n为区间个数