mediumBacktrackingMicrosoftLinkedInAmazon
Lineup Shuffler
A scheduling coach enumerates every ordering of a roster for alternate practice lineups.
Given an array of distinct integers, return all possible permutations.
Choose approach
1
0i
2
13
2SetPermutations (0)
empty
1/22
Narrationstep 1 of 22
Begin DFS; try every unused element at each depth.
Variables
cur=[]
out=0
How to think about it
- n! leaves — unavoidable.
- `used` flag array prevents reuse at a given depth.
- Backtrack: pop and unflag after recursion returns.
Edge cases to catch in interview
- n = 0 -> single empty permutation.
- Duplicates NOT in input (that's a different problem).
Examples
Example 1
6 orderings{"values":[1,2,3]}
Try your own input
SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed