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.
- A. 266
- B. 265
- C. 455
- 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
- Can the numbers 1 through 20 be split into two groups with equal sums? Answer 1 if yes and 0 if no, computed with a subset-sum DP over bits.
- From the eight distinct values [4, 9, 11, 2, 7, 13, 5, 8], how many 4-element subsets sum to exactly 20? Use meet in the middle on the two…
- The eight values [3, 8, 1, 9, 4, 6, 7, 2] are divided into two groups of any sizes. What is the minimum possible absolute difference of…
- Busy intervals [2,5), [4,7), [9,12), [11,13), [15,16) lie inside the working day [0, 20). How many time units in the day are free, i.e…
- Closed intervals [1,4], [2,6], [5,8], [7,9], [8,12], [10,11] must each contain at least one chosen integer point. What is the minimum…
- Jobs are triples (start, end, weight): (1,3,2), (2,5,4), (4,6,3), (6,8,5), (5,9,6). Jobs may be chosen together only when one ends no…