Data Structures & AlgorithmsModerateTCS NQT · Infosys

Problem: The sequence x0 = 1 and x(n+1) = (3·x(n) + 1) mod 7 is generated. The first repeated value appears at some step. What is the number of distinct values in the sequence before the first repeat (including x0)?

  1. A. 7
  2. B. 5
  3. C. 6
  4. D. 12

Correct answer: C. 6

The values are 1, 4, 6, 5, 2, 0, and then 3·0 + 1 = 1 repeats, so 6 distinct values (3 is never reached). Assuming every residue appears gives 7, leaving out x0 gives 5, and running two cycles gives 12.

More Coding Interview Problems questions

Exams that ask this topic