Question d’entretien chez Tinder

Find the max k elements in an unsorted array.

Réponses aux questions d'entretien

Utilisateur anonyme

20 juil. 2016

Build maxheap and extract first k elements

2

Utilisateur anonyme

29 août 2016

The best way would be to use the common rank finding algorithm. Find the rank # of elements - k. In the step where k is found look to the right and those are the tops. O(n) time.

1

Utilisateur anonyme

24 juin 2016

You could sort but there is a more efficient way. You could use a priority queue or have an array of length of k, copy the first k elements of the array, sort them and then read the rest of the elements of the original array and swap elements as necessary.

1

Utilisateur anonyme

6 mars 2017

Quickselect seems appropriate with O(n).