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?
  1. A. 36
  2. B. 21
  3. C. 11
  4. 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

Exams that ask this topic