Data Structures & AlgorithmsModerateTCS NQT · Infosys

Problem: The sequence a0 = 0 and a(n) = a(n−1) + (n mod 3) for n ≥ 1 is defined. What is a(30)?

  1. A. 30
  2. B. 465
  3. C. 60
  4. D. 0

Correct answer: A. 30

Each block n ≡ 1, 2, 0 adds 1 + 2 + 0 = 3, and 1..30 is 10 blocks, so a(30) = 30. Forgetting the mod gives 465, using 1 + 2 + 3 per block gives 60, and 0 is just 30 mod 3.

More Coding Interview Problems questions

Exams that ask this topic