Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: How many integers in [1, 1000] are both perfect squares and perfect cubes? Run the code.

Problem: How many integers in [1, 1000] are both perfect squares and perfect cubes? Run the code.
print(sum(1 for x in range(1, 1001) if any(k * k == x for k in range(1, 32)) and any(k ** 3 == x for k in range(1, 11))))
What is printed?
  1. A. 4
  2. B. 2
  3. C. 3
  4. D. 5

Correct answer: C. 3

A number that is both a square and a cube is a sixth power, and the sixth powers up to 1000 are 1, 64 and 729.

More Coding Interview Problems questions

Exams that ask this topic