Data Structures & AlgorithmsModerateAmazon · Microsoft OA

Generating all permutations of n distinct items with backtracking and copying each complete permutation into the result list costs how much time?

  1. A. Θ(n!)
  2. B. Θ(n · n!)
  3. C. Θ(2ⁿ)
  4. D. Θ(n² · 2ⁿ)

Correct answer: B. Θ(n · n!)

There are n! permutations and copying each takes Θ(n), so output alone is Θ(n · n!). The recursion tree's internal nodes add only a constant factor (about e · n! nodes in total).

More Coding Interview Problems questions

Exams that ask this topic