Data Structures & AlgorithmsModerateProduct Company SDE · CP practice

Problem: What is the sum of the first 12 terms of the sequence an = n^2 + 1 (n from 1 to 12)? Run the code.

Problem: What is the sum of the first 12 terms of the sequence a_n = n^2 + 1 (n from 1 to 12)? Run the code.
print(sum(n * n + 1 for n in range(1, 13)))
What is printed?
  1. A. 662
  2. B. 650
  3. C. 517
  4. D. 818

Correct answer: A. 662

Σ n² for n = 1..12 is 12 · 13 · 25 / 6 = 650, and the twelve + 1 terms add 12, giving 662. Forgetting the + 1s gives 650, stopping at n = 11 gives 517, and summing (n + 1)² gives 818.

More Coding Interview Problems questions

Exams that ask this topic