Amazon Interview Question

Given an array of integers, return the most frequent integer.

Interview Answers

Anonymous

Apr 5, 2015

I started with o(n2) solution and o(n) space. And I wrote code for that. Then he asked me to optimize the code. I was able to optimize it to time O(n) and space O(n). Then he asked me to optimize the space for O(1). I couldn't think of anything. By this time it was almost 60min. The interviewer was not so happy since I he wanted O(1) space. After 5 days I got a reject.

Anonymous

May 16, 2016

I suppose the O(1) space solution would be sort the input array. Then walk it and store two values (the count of the current value and the value itself). Only change current value is the count of another value surpasses it. Then return that.