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

How many permutations of (1, 2, 3, 4, 5) have every pair of adjacent entries differing by at least 2? Solve with a bitmask DP that remembers the last value placed.

  1. A. 28
  2. B. 18
  3. C. 120
  4. D. 14

Correct answer: D. 14

The state is (set of used values, last value). Each permutation is counted once. The count 14 comes from the DP and matches brute force; allowing a difference of 1 makes adjacent values acceptable and yields a much larger number.

More Coding Interview Problems questions

Exams that ask this topic