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?
- A. Θ(n!)
- B. Θ(n · n!)
- C. Θ(2ⁿ)
- 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
- A disjoint-set forest uses path compression but NO union by rank/size. What is its amortised cost per operation over m operations on n…
- What is the running time of Dijkstra's algorithm with a Fibonacci heap on a graph with V vertices and E edges?
- For an array of n = 10⁶ integers, report the number of DISTINCT values in every window of k = 10⁵ consecutive elements. Which approach is…
- n = 10⁵ points are given, followed by q = 10⁵ OFFLINE queries 'how many points lie in the axis-aligned rectangle [x1, x2] × [y1, y2]?'…
- Edge case: integer division and modulo with negative operands in Python. python print(-7 // 2, -7 % 2, int(-7 / 2)) What is printed?
- Problem: Replace at most k characters so that the longest possible substring consists of one repeated letter; return its length. python…