Optiver Interview Question

Third largest number in an array

Interview Answers

Anonymous

Oct 18, 2017

Use 3 integer variables to store first, second, and third largest values (set them all to integer.min). Iterate once through the list and keep track of all three using conditions. Return third largest.

Anonymous

Nov 24, 2017

This is the answer to a more general problem where we are asked to find the nth element and not necessarily the 3rd. 1. If this is asked in the coding test, and you happen to use C++, you are in luck. Use the nth_element function 2. max-heapify the array and pop it k-1 times. The top of the heap is the answer. 3. create a min-heap using first k elements of the array. for the remaining elements, push it into the heap and pop the min. Do for all the remaining elements. In the end, you are left with a heap containing k elements. The top of the heap is the element you are looking for.

1