Question d’entretien chez Google

Select K largest number from N

Réponses aux questions d'entretien

Utilisateur anonyme

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

Utilisateur anonyme

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

Utilisateur anonyme

30 avr. 2012

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

1

Utilisateur anonyme

3 mai 2012

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

Utilisateur anonyme

29 avr. 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)