LinkedIn Interview Question

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

Interview Answers

Anonymous

Aug 23, 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

Anonymous

Feb 19, 2019

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

Anonymous

Aug 20, 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.