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?
- A. 662
- B. 650
- C. 517
- 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
- Problem: How many 4-letter words over {x, y, z} contain at least one letter from each of the three symbols? Run the code. python from…
- Problem: What is the number of integers x in [1, 200] for which x^2 + 2x + 1 is divisible by 7? Run the code. python print(sum(1 for x in…
- Problem: How many integer solutions (x, y) satisfy x^2 + y^2 = 65 with x, y >= 0? Run the code. python print(sum(1 for x in range(0, 9)…
- Problem: What is the maximum length of a strictly decreasing subsequence of [9, 4, 6, 3, 8, 1] ? Run the code. python a = [9, 4, 6, 3, 8…
- Problem: How many numbers in [1, 1000] are divisible by 3 and whose digit sum is also divisible by 3? Run the code. python print(sum(1 for…
- Problem: What is the time complexity of finding all prime factors of n up to 10^12 by trial division?