I knew the algorithm conceptually but fumbled the index range on the randint call.
Start by clarifying the problem and constraints, then explain the Fisher-Yates algorithm and its O(n) time and O(1) space complexity. Walk through the code step by step, emphasizing the loop from the end to the beginning and the random index selection. Finally, discuss correctness and potential pitfalls like off-by-one errors.
Pro tip: Mention that you can optimize by iterating from the end and swapping with a random index in [0, i], which avoids bias and is the modern version of the algorithm. Also, note that using a basic random integer generator means you need to ensure it produces uniformly distributed integers in the given range.
Confirm that the input is an integer array, the output should be a shuffled array (in-place or new), and that only a basic random integer generator is allowed. Ask about edge cases like empty array or single element.
Describe the algorithm: iterate from the last element to the second element, pick a random index from 0 to the current index, and swap the current element with the randomly chosen one. Emphasize that this produces an unbiased permutation.
Write code (or pseudocode) that uses a random integer generator to pick an index in the range [0, i]. Show the swap operation and ensure the loop bounds are correct.
State that time complexity is O(n) and space complexity is O(1) if shuffling in-place. Explain why each permutation is equally likely, and mention that the algorithm is unbiased.
Walk through a small example (e.g., [1,2,3]) to demonstrate the steps. Discuss edge cases like empty array, single element, and potential issues with the random generator (e.g., modulo bias).
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.