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.
- A. 32
- B. 17
- C. 16
- 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
- Choose any subset (empty allowed) of {1, 2, 3, 4, 5}. How many subsets have bitwise OR equal to 7?
- How many binary strings of length 8 contain no three consecutive 1s?
- Array [5, 2, 8, 1, 4, 6, 3]. How many index pairs i < j have a[i] AND a[j] = 0 (no common set bit)?
- What is the minimum number of single-bit flips needed to change the 8-bit number 13 into the 8-bit number 22?
- Consider the eight primes 2, 3, 5, 7, 11, 13, 17, 19. How many subsets (including the empty subset, whose product is 1) have a product…
- How many 3-element subsets of {1, 2, 3, 4, 5, 6, 7, 8} have a sum divisible by 6?