## Facebook Interview Question

Software Engineers**Country:**United States

**Interview Type:**In-Person

public void test() {

int[] items = { 11, 30, 2, 30, 30, 30, 6, 2, 62, 62 };

List<Integer> maxIndexes = new ArrayList<Integer>();

int max = 0;

for (int i = 0; i < items.length; i++) {

int maxIndex = 0;

if (items[i] > max) {

maxIndexes.clear();

maxIndexes.add(i);

max = items[i];

} else if (items[i] == max) {

maxIndexes.add(i);

}

System.out.println(maxIndexes.get(new Random().nextInt(maxIndexes

.size())));

}

}

```
/*
In the current form, the question is ill posed.
It is as good as asking, given an array, find which indices
the maximal elements are - and return one at random.
More realistic and proper question would be:
Given a stream of integers, at any given time, if we were to ask
Give me a random index where the maximal element has appeared.
That would be worth interesting effort.
So we start.
*/
def bucket : { val : num('-inf'), indices : list(-1), cur_index : -1 }
def find_random_index( integer_iterator , bucket ){
if ( integer_iterator.hasNext ){
bucket.cur_index += 1
next_int = integer_iterator.next()
if ( next_int > bucket.val ){
bucket.val = next_int
bucket.indices = list( bucket.cur_index )
} else if ( next_int == bucket.val ){
bucket.indices += bucket.cur_index
}
}
random( bucket.indices ) // return a random value from collection
}
```

Javascript solution

```
const findRadomMatchingIndex = arr => {
const matchesMap = {};
let max = Number.MIN_VALUE;
arr.forEach((el, idx) => {
if (el > max) {
max = el;
}
matchesMap[el] ? matchesMap[el].push(idx) : matchesMap[el] = [idx];
});
return matchesMap[max][Math.floor(Math.random() * matchesMap[max].length)]
}
```

Here is my proposal in Python:

```
from random import randint
def parseArray(a = [11, 30, 2, 30, 30, 30, 6, 2, 62, 62]):
max_value = 0
for i in range(len(a)):
if a[i] > max_value:
max_value = a[i]
max_indexes = []
if a[i] == max_value:
max_indexes.append(i)
print('Seen so far: {}'.format(a[:i+1]))
print('Max: {}'.format(max_value))
print('Max indexes: {}'.format(max_indexes))
print('Random max index: {}'.format(
max_indexes[randint(0, len(max_indexes) - 1)]))
print('')
```

Here is my proposal in Python:

```
from random import randint
def parseArray(a = [11, 30, 2, 30, 30, 30, 6, 2, 62, 62]):
max_value = 0
for i in range(len(a)):
if a[i] > max_value:
max_value = a[i]
max_indexes = []
if a[i] == max_value:
max_indexes.append(i)
print('Seen so far: {}'.format(a[:i+1]))
print('Max: {}'.format(max_value))
print('Max indexes: {}'.format(max_indexes))
print('Random max index: {}'.format(
max_indexes[randint(0, len(max_indexes) - 1)]))
print('')
```

Looking for interview experience sharing and mentors?

- acoding167 March 27, 2019Visit A++ Coding Bootcamp at aonecode.com.

Taught by experienced engineers/interviewers from FB, Google and Uber,

our ONE TO ONE courses cover everything in an interview including

latest interview questions sorted by companies,

SYSTEM DESIGN Courses (highly recommended for people interviewing with FLAG)

ALGORITHMS (conquer DP, Graph, Greed and other advanced algo problems),

and mock interviews.

Our students got offers from Google, Uber, Facebook, Amazon and other top companies after a few weeks of training.

Welcome to email us with any questions. Thanks for reading.