Yahoo interview question

Find the kth largest element in an array

Interview Answers

Anonymous

Mar 21, 2017

Why not use a PriorityQueue and solve?

1

Anonymous

Feb 24, 2017

Initiate an list of k elements maximum length . Move through the array, tracking a max index of the queue and min index . While iterating, add the array item if the list has less than k items. If it does have more than k items, swap the smallest value in the list out with the max value, and realign the max and min value pointers. When the array is fully iterated through, sort the list and find the kth largest value.

Anonymous

Feb 24, 2017

Initiate an list of k elements maximum length . Move through the array, tracking a max index of the queue and min index . While iterating, add the array item if the list has less than k items. If it does have more than k items, swap the smallest value in the list out with the max value, and realign the max and min value pointers. When the array is fully iterated through, sort the list and find the kth largest value.