Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Problem: How many permutations of 0..4 have exactly one inversion? Run the code.
Problem: How many permutations of 0..4 have exactly one inversion? Run the code.
from itertools import permutations print(sum(1 for p in permutations(range(5)) if sum(1 for i in range(5) for j in range(i + 1, 5) if p[i] > p[j]) == 1))What is printed?
- A. 5
- B. 4
- C. 10
- D. 3
Correct answer: B. 4
A permutation with exactly one inversion swaps one adjacent pair, and there are 4 adjacent pairs.
More Coding Interview Problems questions
- Problem: What is the smallest positive integer with exactly 10 positive divisors? Run the search. python n = 1 while sum(1 for d in…
- Problem: How many integers n in [1, 100] reach 1 under the Collatz map within 10 steps? Run the code. python def steps(n): c = 0 while n…
- Problem: How many ordered pairs (a, b) with 1 <= a, b <= 30 have a^2 + b^2 a perfect square? Run the code. python print(sum(1 for a in…
- Problem: How many distinct strings can be formed by rearranging the letters of 'aabbc'? Run the code. python from itertools import…
- Problem: A 3-person committee is chosen from 6 people. How many committees include at least one of two specific people A or B? Run the…
- Problem: How many non-negative integer solutions (x, y, z) of x + y + z = 10 satisfy x <= 4? Run the code. python print(sum(1 for x in…