Data Structures & AlgorithmsHardICPC regional · GATE CS
Problem: How many permutations of the letters A, B, C, D leave no letter in its original position (for example, A is not in the first place, B is not in the second, and so on)?
- A. 24
- B. 6
- C. 12
- D. 9
Correct answer: D. 9
D(4) = 4!(1 − 1 + 1/2 − 1/6 + 1/24) = 9 (or D(n) = (n − 1)(D(n−1) + D(n−2)) = 3 · (2 + 1)). 24 counts all permutations, 6 is 3!, and stopping inclusion-exclusion after the pair terms gives 24 − 24 + 12 = 12.
More Coding Interview Problems questions
- Problem: The sum of all integers from 1 to 200 that are divisible by neither 4 nor 7 is what value?
- Problem: How many integers from 1 to 100 inclusive are divisible by 7 but not by 14?
- Problem: What is the least common multiple of 6, 8 and 12?
- Problem: How many trailing zeros does 30! have in base 10?
- Problem: How many unordered pairs {x, y} with x ≠ y from the set {1, 2, 3, 4, 5} have x + y even?
- Problem: How many binary strings of length 4 contain no two adjacent 1s?