I knew Fisher-Yates but blanked for a second on why you swap in-place instead of building a separate structure.
Start by clarifying the problem constraints and the random integer function's behavior. Then, describe the Fisher-Yates shuffle algorithm, emphasizing its O(n) time and O(1) extra space (in-place) properties. Walk through the algorithm step-by-step, proving uniformity and analyzing complexity.
Pro tip: Mention that the algorithm is in-place and that the random integer function must generate unbiased random numbers in the correct range; this shows attention to detail and practical implementation concerns.
Confirm that the permutation must be uniformly random, O(n) time, and minimize extra space. Ask about the random integer function's signature and whether it can generate numbers in a given range.
Select the Fisher-Yates shuffle (also known as Knuth shuffle) because it guarantees uniform random permutations in O(n) time and O(1) extra space when implemented in-place.
Describe iterating from the last index down to 1, and for each index i, swapping the element at i with the element at a randomly chosen index j where 0 ≤ j ≤ i. Emphasize that each swap uses the random integer function to pick j uniformly.
Argue that each permutation is equally likely by showing that the probability of any specific permutation is 1/n!. State that the loop runs n-1 times, each iteration doing O(1) work, resulting in O(n) time and O(1) extra space.
Mention handling of n=0 or n=1, and note that the random integer function should be unbiased. If needed, discuss how to adapt if the function only generates numbers in a fixed range (e.g., using modulo with rejection sampling).
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.