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.
- A. 28
- B. 18
- C. 120
- 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
- How many integers x with 0 <= x <= 1000 have exactly four 1-bits in their binary representation?
- Among the values [3, 10, 5, 25, 2, 8], what is the maximum value of x XOR y over two different elements x and y?
- From the values [5, 12, 7, 3, 9, 14, 2, 8], what is the minimum number of values you must choose so that their sum is at least 25? Meet in…
- Arrays A = [3, 7, 11, 15, 20] and B = [2, 6, 9, 13, 18] each hold distinct values. How many pairs (x in A, y in B) satisfy x + y = 22?
- Half-open intervals [1,6), [2,4), [3,8), [5,7), [6,9), [7,10) are given. What is the maximum number of intervals that contain a common…
- Trains (arrival, departure) in minutes are (900,930), (940,1200), (950,1120), (1100,1130), (1500,1800), (1130,1140). A platform holds one…