Data Structures & AlgorithmsModerateCAT · TCS NQT

Problem: How many integers from 1 to 1000 are perfect squares or perfect cubes (counted once even if both)?

  1. A. 41
  2. B. 31
  3. C. 35
  4. D. 38

Correct answer: D. 38

31 squares + 10 cubes − 3 sixth powers (1, 64, 729) = 38. Not removing the overlap gives 41, squares alone give 31, and subtracting the overlap twice gives 35.

More Coding Interview Problems questions

Exams that ask this topic