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

How many permutations (p1, ..., p5) of the numbers 1 to 5 satisfy p_i <= i + 1 for every position i? Use a bitmask DP over the set of values already placed.

  1. A. 32
  2. B. 17
  3. C. 16
  4. D. 22

Correct answer: C. 16

The state is the set of values used; position i = popcount + 1 can hold any unused value at most i + 1. The DP gives 16, which matches brute force. Counting all 2^n subsets of values, or doubling, ignores the placement constraint.

More Coding Interview Problems questions

Exams that ask this topic