Frage im Vorstellungsgespräch bei Jane Street

Output the random permutation for a given string

Antworten zu Vorstellungsgespräch

Anonym

29. Jan. 2012

I came up with an O(n) algorithm, but the interviewee asked more

Anonym

4. Okt. 2012

This can be done optimally in O(n) time by iterating over the string, swapping the character at index i with the character at a random index each time.

Anonym

1. Nov. 2017

It's not possible to do asymptotically better than O(n) because on average you'll have to change n-1 of the characters (assuming they're all distinct).