Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Problem: How many distinct products a b arise when a and b are each a face value from 1 to 6? Run the code.
Problem: How many distinct products a * b arise when a and b are each a face value from 1 to 6? Run the code.
print(len({a * b for a in range(1, 7) for b in range(1, 7)}))What is printed?- A. 36
- B. 21
- C. 11
- D. 18
Correct answer: D. 18
The set of products of 1..6 has 18 values, because many collide (2·6 = 3·4, 1·4 = 2·2, …). 36 counts ordered pairs, 21 counts unordered pairs, and 11 is the number of distinct sums.
More Coding Interview Problems questions
- 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). python…
- 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?