Data Structures & AlgorithmsVery hardProduct company OA · Amazon/Microsoft-style OA

How many permutations p of {0, 1, 2, 3, 4, 5} satisfy p[i] != i for every position i? Use a bitmask DP over the set of values already placed.

  1. A. 266
  2. B. 265
  3. C. 455
  4. D. 530

Correct answer: B. 265

Positions are filled in order, and the state is the set of values used so far, with the current position equal to the popcount. Forbidding p[i] = i removes exactly one value per position; the count for n = 6 is 265, and the complement 720 - 265 counts permutations with at least one fixed point, which is the trap.

More Coding Interview Problems questions

Exams that ask this topic