Data Structures & AlgorithmsHardTCS NQT Digital · Product Company SDE

Problem: A sequence is defined by a1 = 1, a2 = 1, and an = an−1 + an−2 mod 7 for n ≥ 3. What is a30 (1-indexed)?

  1. A. 6
  2. B. 2
  3. C. 1
  4. D. 832040

Correct answer: A. 6

Fibonacci numbers mod 7 repeat with Pisano period 16, so a30 = a14 = 377 mod 7 = 6. Shifting the index by one gives a29 = 2 or a31 = 1, and forgetting the reduction leaves F30 = 832040.

More Coding Interview Problems questions

Exams that ask this topic