Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: What is the number of integers in [1, 10000] that are perfect squares and also end in the digit 9? Run the code.

Problem: What is the number of integers in [1, 10000] that are perfect squares and also end in the digit 9? Run the code.
print(sum(1 for x in range(1, 10001) if int(round(x ** 0.5)) ** 2 == x and x % 10 == 9))
What is printed?
  1. A. 20
  2. B. 10
  3. C. 100
  4. D. 1000

Correct answer: A. 20

A square ends in 9 only if its root ends in 3 or 7; roots 1..100 give 10 + 10 = 20. Counting only roots ending in 3 gives 10, 100 counts all squares, and 1000 counts all numbers ending in 9.

More Coding Interview Problems questions

Exams that ask this topic