Domanda di colloquio di Google

Select K largest number from N

Risposte di colloquio

Anonimo

4 mag 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

Anonimo

6 lug 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.

Anonimo

30 apr 2012

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

1

Anonimo

3 mag 2012

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

Anonimo

29 apr 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)