Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: How many ways are there to write 50 as an ordered sum a^2 + b^2 with a, b >= 0? Run the code.

Problem: How many ways are there to write 50 as an ordered sum a^2 + b^2 with a, b >= 0? Run the code.
print(sum(1 for a in range(0, 8) for b in range(0, 8) if a * a + b * b == 50))
What is printed?
  1. A. 4
  2. B. 3
  3. C. 2
  4. D. 5

Correct answer: B. 3

Only a, b in 0..7 can work; the pairs (1,7), (7,1), (5,5) give the ordered count.

More Coding Interview Problems questions

Exams that ask this topic