Data Structures & AlgorithmsModerateTCS NQT · Infosys
Problem: The Collatz sequence starts at n = 27 and repeatedly applies: n → n/2 if n is even, n → 3n+1 if n is odd, until n = 1. How many steps does it take to reach 1?
- A. 112
- B. 9232
- C. 111
- D. 41
Correct answer: C. 111
Simulating 27 → 82 → 41 → … → 1 takes 111 steps (it climbs as high as 9232 on the way). Counting the terms including 27 gives 112, 9232 is the peak value rather than a step count, and counting only the 3n + 1 steps gives 41.
More Coding Interview Problems questions
- Problem: For the same Collatz process starting at n = 27, what is the largest value that appears in the sequence?
- Problem: The digital root of a positive integer is obtained by repeatedly summing its decimal digits until a single digit remains. What is…
- Problem: How many integers n with 1 ≤ n ≤ 1000 have decimal digits summing to exactly 10?
- Problem: What is the smallest positive integer n such that n! is divisible by 10⁶ (that is, n! has at least six trailing zeros in base 10)?
- Problem: Children stand in a line with ratings [1, 0, 2]. Each child gets at least one candy, and a child with a higher rating than a…
- Problem: Find the maximum amount of water held between two vertical lines of heights [1, 8, 6, 2, 5, 4, 8, 3, 7] (the water is limited by…