This problem follows the Swap/Used Array pattern, commonly found in the Backtracking category. Recognizing this pattern is key to solving it efficiently in an interview setting.
Fix each element at current position. Recurse for remaining. Track used elements.
Unlike subsets where order doesn't matter, permutations always start from index 0 — you skip already-used elements instead of incrementing the start index.
result = []
def backtrack(current):
if len(current) == len(nums):
result.append(current[:])
return
for num in nums:
if num in current: continue # or use 'used' array
current.append(num)
backtrack(current)
current.pop()
backtrack([])
return resultPractice Permutations and similar Backtracking problems with flashcards. Build pattern recognition through active recall.
Practice this problem