Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice

Problem: How many derangements (permutations with no fixed point) of 1..6 exist? Run the code.

Problem: How many derangements (permutations with no fixed point) of 1..6 exist? Run the code.
from itertools import permutations
print(sum(1 for p in permutations(range(6)) if all(p[i] != i for i in range(6))))
What is printed?
  1. A. 265
  2. B. 264
  3. C. 120
  4. D. 720

Correct answer: A. 265

The derangement count D(6) = 265 comes from inclusion-exclusion over fixed points; 120 = 5! and 720 = 6! are the traps.

More Coding Interview Problems questions

Exams that ask this topic