Pergunta de entrevista da empresa Google

Select K largest number from N

Respostas da entrevista

Sigiloso

4 de mai. de 2012

The answer is something called quickselect. It's similar to quicksort, only that once an array is partitioned, you simply ignore the portion that does not contain the Kth position. You aim for the pivot to fall on the Kth position. Interestingly enough, the complexity for an average case is O(n).

4

Sigiloso

6 de jul. de 2012

As mentioned previously, find the Kth largest element in linear time (using median of medians) and then go over the array to find those that are larger than it, also in linear time.

Sigiloso

30 de abr. de 2012

To use a heap other than list, will improve the performance

1

Sigiloso

3 de mai. de 2012

I would sort it using quicksort then return the n-5 element.

Sigiloso

29 de abr. de 2012

Keep a list of K elements. Go through N, and adjust the list whenever you find a new element that is larger than any element in the K list. Time complexity is O(K*N)