Pregunta de entrevista de Google

Select K largest number from N

Respuestas de entrevistas

Anónimo

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

Anónimo

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

Anónimo

30 abr 2012

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

1

Anónimo

3 may 2012

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

Anónimo

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