Tinder interview question

Find the max k elements in an unsorted array.

Interview Answers

Anonymous

20 Jul 2016

Build maxheap and extract first k elements

2

Anonymous

29 Aug 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

Anonymous

24 Jun 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

Anonymous

6 Mar 2017

Quickselect seems appropriate with O(n).