Permutations is one of those problems where you either know the recursive backtracking pattern cold or you're drawing blanks on a whiteboard.
Start by clarifying the problem: whether the input has duplicates, whether the output should be unique permutations, and whether to return a list or print. Then explain a backtracking approach that builds permutations by swapping elements in-place, and analyze time and space complexity. If duplicates are possible, mention how to handle them (e.g., using a set or sorting and skipping duplicates).
Pro tip: At Google, interviewers value clean, bug-free code and strong communication. Before coding, walk through a small example (e.g., [1,2,3]) to demonstrate your thought process, and after coding, test edge cases like empty input, single element, and duplicates.
Ask about input size, duplicates, output format, and whether permutations should be unique. This shows attention to detail and avoids incorrect assumptions.
Decide between backtracking with swapping, backtracking with a used array, or iterative generation. For interviews, swapping is often preferred for its simplicity and O(1) extra space (excluding recursion stack).
Describe how you'll recursively fix each position by swapping the current index with each subsequent index, then recurse. Base case: when the current index reaches the end, add the permutation to the result.
If duplicates exist, either use a set to store results or sort the array and skip swapping when the same element has already been placed at the current position.
State time complexity O(n * n!) and space O(n) for recursion (plus output). Walk through a small example and test edge cases like empty input, single element, and duplicates.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.