Coding interview problems › Backtracking
🧩 Backtracking Medium
Permutations
Permutations explained: generate all n! orderings with backtracking and a used[] array, tested Python, the classic copy and undo bugs, and a live visual.
Also searched as: all permutations of an array backtracking · generate permutations recursion · print all permutations
Try it live Free
Backtracking: Subsets, Permutations & Pruning
Switch the lab to permutations and watch the recursion tree grow to n! leaves, one choose, explore, un-choose step at a time.
Also useful: Recursion & the Call Stack
The problem
Given a list of distinct integers, return every possible ordering of all of them. Any order of the output is fine.
3! = 6
The key insight
Fill the positions one at a time. For the next position you may place any number not yet used. Mark it used, recurse to fill the rest, then unmark it so the next branch can use it. When the path has all n numbers, it is one complete permutation.
Step-by-step approach
- Keep
path, a boolean arrayused, and the output list. - If
len(path) == n, store a copy and return. - Otherwise, for each index
inot yet used: mark it, appendnums[i], recurse. - After the call returns, pop it and unmark it (un-choose).
- Return the output after
dfs()finishes.
Python solution
def permute(nums):
out, path = [], []
used = [False] * len(nums)
def dfs():
if len(path) == len(nums):
out.append(path[:])
return
for i, x in enumerate(nums):
if used[i]:
continue
used[i] = True # choose
path.append(x)
dfs() # explore
path.pop() # un-choose
used[i] = False
dfs()
return outComplexity
There are n! permutations and each one costs O(n) to copy. The recursion depth, path and used are O(n) apart from the output.
Common mistakes
- Not resetting
used[i]after the recursive call, so later branches think the number is still taken. - Storing
pathinstead ofpath[:]. - With duplicate values, producing repeated permutations. For Permutations II, sort and skip a value equal to the previous one when the previous one is not currently used.
Frequently asked questions
What is the time and space complexity of generating permutations?
O(n × n!) time, since there are n! permutations of length n. Extra space is O(n) for recursion, path and the used array, plus the output.
How do I avoid duplicate permutations when the input has repeats?
Sort the input. While looping, skip nums[i] if it equals nums[i - 1] and used[i - 1] is False; this forces equal values to be placed in a fixed order.
Can I generate permutations by swapping?
Yes. For position k, swap each element from k onward into position k, recurse on k + 1, then swap back. It avoids the used array and works in place.
Preparing for placements?
Take the 2-minute readiness check to find your weak patterns, then follow the placement path from DSA core to system design.
All coding interview problems More Backtracking Backtracking: Subsets, Permutations & Pruning lab Placement prep guides
Problem statements on this site are written in our own words. Problem names are the common names used across interview-prep communities; BitBootcamp is an independent learning platform and is not affiliated with LeetCode or any company that asks these questions.