Bloomberg Interview Question

How to find the medium number of a given array.

Interview Answers

Anonymous

Sep 19, 2010

Randomized quickselect is usually the easiest to implement http://en.wikipedia.org/wiki/Selection_algorithm

1

Anonymous

Sep 27, 2010

First sort the given set - O(n logn) Then if the size is odd, the middle element if the size is even, then its the average of the two middle elements

1

Anonymous

Oct 17, 2010

you could do that in linear time if the array size is less than 5 - easy to find if more than 5, read first 5 elements. Eliminate the smallest and the largest. Now you have 3 elements. Read another 2 elements, compare to your 3 elements that you've read before. Eliminate smallest and largest. Continue doing that until you have less than 5 elements left and then easily find the middle element among the remaining ones.