Domanda di colloquio di LinkedIn

Given an array of integers, write a function that will produce a random permutation of the input array.

Risposte di colloquio

Anonimo

23 ago 2017

To ensure a uniform distribution; for each index in the new array; every "unpicked" member of the old array must have an equal chance of being chosen public static void generateRandomSequence(int[] arr){ Random random = new Random(); for (int i =0;i

4

Anonimo

19 feb 2019

Use Fisher–Yates shuffle Algorithm to get uniform random array.

Anonimo

20 ago 2017

I wrote an algorithm that would pick two indices of the input array at random and swap them some number of times at least as large as the size of the input array. The interviewer argued that this would not produce a uniformly distributed permutation of the input array. I am still unsure if this is correct, but either way, I think it's a pretty nitpicky criticism of the answer.