Problem, as an interviewer would state it:
"Create shuffle(array) that returns a uniformly random permutation in O(n) without bias. Do not use sort with Math.random."
Examples:
shuffle([1, 2, 3, 4, 5]); // e.g. [3, 1, 5, 2, 4], every ordering equally likely
Clarifying questions expected:
- Must it shuffle in place, or return a new array?
- What does "unbiased" mean precisely — every one of the n! permutations equally likely?
- Does it need a seedable or injectable random source for testing?
Code / implementation expected: Yes — real, direct measurement of the output distribution for the correct algorithm AND for the two tempting wrong ones.