3

I came across this coding problem and am having a hard time coming up with a solution.

Given an array of integers, find the most common element in a list of integers and return any of its indexes randomly with equal probability. The solution must run in O(N) time and use O(1) space.

Example:

List contains: [-1, 4, 9, 7, 7, 2, 7, 3, 0, 9, 6, 5, 7, 8, 9]

7 is most common element so output should be one of: 3, 4, 6, 12

Now this problem would be fairly trivial if not for the constant space constraint. I know reservoir sampling can be used to solve the problem with these constraints if we know the the most common element ahead of time. But if we don't know the most common element, how could this problem be solved?

SW Williams
  • 559
  • 1
  • 5
  • 18
  • Do you know anything about the integers? Are they constrained to be within some fixed range (e.g. 32-bit integers), or can they be arbitrary integers with any number of digits? – templatetypedef Apr 14 '20 at 00:14
  • Can we iterate over given array multiple times? – fas Apr 14 '20 at 00:17
  • 2
    @fas There's an impossibility result that says finding the most common element in an array of arbitrary elements can't be done in one pass and constant space. So either multiple passes are required or the elements can't be arbitrary. – templatetypedef Apr 14 '20 at 00:19
  • One thing to think about: if the bounds' N is the array size and the integers can be of arbitrary size, then even inspecting the elements requires more than O(N) time on any real computer. Consequently, I think it's fair to assume there's a size bound on the ints. – Gene Apr 14 '20 at 00:25
  • @Gene Assuming numbers have fixed size bounds we can make size bound iterations over array, which gives _O(N)_ complexity (with a huge constant) and _O(1)_ space, but that's seems very unfair. – fas Apr 14 '20 at 00:33
  • Integer size can be assumed to be arbitrary, like Python integers. The one hint I was given is to use reservoir sampling. Is there a way to utilize reservoir sampling to work around the most common element problem? – SW Williams Apr 14 '20 at 01:49
  • @SWWilliams As I said, if integers can be any length, then to read the array one time requires O(N log K) for any realistic machine model, where K is the max integer value. A time bound of O(N) is impossible. – Gene Apr 14 '20 at 02:55
  • @Gene The length of integers most probably has an upper bound. You are distracting from the main question. Fix the length of the integer and try to solve for it. – Wasim Ahmad Apr 14 '20 at 09:59
  • Assuming we can solve [this](https://stackoverflow.com/questions/11781720/most-common-element-in-an-array-finding-the-relative-majority-deterministical) question, we can answer your problem in the second pass, at least if there is a unique mode. Using in-place radix sort, we can get the answer in constant _extra space_ an linear time. – hilberts_drinking_problem Apr 15 '20 at 07:45

0 Answers0