I was recently in an interview and the interviewer asked me the following question:
Given an unsorted array, how do you calculate the mode in O(N)?
I answered along the lines of using a hashmap, O(N) loop through array and O(1) lookups.
Then he said
If you had to use constant memory but were allowed more processor time, how would you do it?
I answered 'sort the array and find the longest run, runtime = O(nlgn)
The next question he asked fucked me up..
If you had to use constant memory and linear time how would you do it?
I didn't know how to answer this and he left this for me as an exercise for later. It's been days and I still dunno how to do it.
Can anyone know how to do?>