Skip to content
Preptima
mediumCodingMidSenior

Generate every permutation of an array that may contain duplicates, then tell me how large the search tree is

4 min readUpdated 2026-08-15

Answer

Short answer

Backtracking builds each candidate incrementally and undoes the last choice on return; duplicates are suppressed by sorting and skipping a repeated value whose identical predecessor is unused.

Was this answer helpful?

Likely follow-ups

  • Why does the same skip condition look different for subsets than for permutations?
  • How would you produce the k-th permutation in lexicographic order without generating the first k?
  • Where does the memory go if you must stream a billion permutations rather than return a list?
  • N-Queens prunes hard and still blows up. What does that tell you about the pruning?

Related questions