Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice

Problem: How many integer pairs (x, y) with 1 <= x <= 10 and 1 <= y <= 10 satisfy x y being a perfect cube? Run the code.

Problem: How many integer pairs (x, y) with 1 <= x <= 10 and 1 <= y <= 10 satisfy x * y being a perfect cube? Run the code.
print(sum(1 for x in range(1, 11) for y in range(1, 11) if int(round((x * y) ** (1 / 3))) ** 3 == x * y))
What is printed?
  1. A. 5
  2. B. 8
  3. C. 4
  4. D. 18

Correct answer: B. 8

Cube products: (1,1), (1,8), (8,1), (2,4), (4,2), (3,9), (9,3), (8,8), so 8 ordered pairs. Unordered pairs give 5, requiring both to be cubes gives 4, and testing for squares instead gives 18.

More Coding Interview Problems questions

Exams that ask this topic