Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice
Problem: How many subsets of {1, 2, ..., 6} have a product that is a perfect square? Run the code (the empty set has product 1).
Problem: How many subsets of {1, 2, ..., 6} have a product that is a perfect square? Run the code (the empty set has product 1).
import math print(sum(1 for m in range(1 << 6) if int(round(math.prod([i + 1 for i in range(6) if m >> i & 1] or [1]) ** 0.5)) ** 2 == math.prod([i + 1 for i in range(6) if m >> i & 1] or [1])))What is printed?
- A. 8
- B. 7
- C. 4
- D. 16
Correct answer: A. 8
Square products: {}, {1}, {4}, {1,4}, {2,3,6} and those with 1 or 4 added, 8 in all, which is 2^(6 − 3) for the 3 independent primes 2, 3, 5. Leaving out the empty set gives 7, using only {1, 4} gives 4, and forgetting prime 5 gives 16.
More Coding Interview Problems questions
- Problem: What is the number of distinct values of floor(n / 2) + floor(n / 3) for 1 <= n <= 50? Run the code. python print(len({n // 2 + n…
- Problem: What is the largest sum of a subarray of length at least 2 in [4, -1, 5, -6, 3, 2]? Run the code. python a = [4, -1, 5, -6, 3, 2]…
- Problem: How many paths of length exactly 4 go from vertex 0 to vertex 0 in the complete graph K3? Run the code (walks allowed to repeat…
- Problem: How many ways are there to choose 4 distinct numbers from 1..12 with sum 26? Run the code. python from itertools import…
- Problem: What is the sum of the digits of the number 2^64? Run the code. python print(sum(int(c) for c in str(2 64))) What is printed?
- Problem: How many integers x in [1, 2000] satisfy x^2 ≡ x (mod 10)? Run the code. python print(sum(1 for x in range(1, 2001) if (x x - x)…