Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice
Problem: How many subsets of {1, 2, ..., 10} contain no two consecutive integers?
Problem: How many subsets of {1, 2, ..., 10} contain no two consecutive integers?
from itertools import combinations print(sum(1 for m in range(1 << 10) if all(not ((m >> i) & 1 and (m >> (i + 1)) & 1) for i in range(9))))What is printed?
- A. 89
- B. 233
- C. 144
- D. 123
Correct answer: C. 144
Subsets of a path of 10 elements with no two adjacent satisfy f(n) = f(n − 1) + f(n − 2) with f(1) = 2, f(2) = 3, so f(10) = F(12) = 144. Shifting the Fibonacci index by one gives 89 or 233, and treating the elements as a circle (also forbidding elements 0 and 9 together) gives the Lucas number 123.
More Coding Interview Problems questions
- Problem: How many integers in [1, 1000] are perfect squares or perfect cubes? python print(sum(1 for x in range(1, 1001) if any(k k == x…
- Problem: Greedy graph colouring assigns colours in vertex order 0, 1, 2, 3, 4, 5, giving each vertex the smallest colour (starting at 0)…
- Problem: In the undirected graph with edges 0-1, 0-2, 1-2, 1-3, 2-3, 3-4, how many simple paths (no repeated vertex) go from vertex 0 to…
- Problem: An undirected forest (a graph with no cycles) has 9 vertices and 6 edges. How many connected components does it have?
- Problem: A rooted tree (root 1) has parent pointers: 2→1, 3→1, 4→2, 5→2, 6→4, 7→6, 8→3, 9→8. How many nodes lie on the path between node 7…
- Problem: For the complete binary tree with 10 nodes stored in heap order (node i has children 2i and 2i+1, root is node 1), what is the…