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)?
- A. 7
- B. 5
- C. 6
- 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
- Problem: A ball starts at position 0 on a track from 0 to 10 and moves 3 units per second. At each boundary it reverses direction…
- Problem: The permutation [2, 1, 4, 3] is processed by a single stack: input elements are pushed in order and popped whenever the popped…
- Problem: How many integers from 1 to 500 are coprime to 30 (share no prime factor with 30 = 2·3·5)?
- Problem: What is the sum of all prime numbers less than 30?
- Problem: How many bits are needed to write 10^9 in binary (the number of bits in its binary representation)?
- Problem: Reverse the 8-bit binary representation of 13 (00001101) and report the resulting unsigned value in decimal.