Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice

Problem: What is the sum of the first 50 terms of the sequence 1, 1, 2, 3, 5, ... (Fibonacci numbers F(1) to F(50), with F(1)=F(2)=1)? Run the code.

Problem: What is the sum of the first 50 terms of the sequence 1, 1, 2, 3, 5, ... (Fibonacci numbers F(1) to F(50), with F(1)=F(2)=1)? Run the code.
a, b = 1, 1
total = 1 + 1
for _ in range(48):
    a, b = b, a + b
    total += b
print(total % 1000)
What is printed?
  1. A. 99
  2. B. 98
  3. C. 25
  4. D. 73

Correct answer: B. 98

Σ F(1..50) = F(52) − 1, and F(52) ends in …099, so the output is 98. Forgetting the − 1 gives 99, F(50) mod 1000 gives 25, and summing only 49 terms gives 73.

More Coding Interview Problems questions

Exams that ask this topic