Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Problem: What is the number of steps for the Collatz sequence starting at 97 to reach 1? Run the code.
Problem: What is the number of steps for the Collatz sequence starting at 97 to reach 1? Run the code.
n = 97
c = 0
while n != 1:
n = n // 2 if n % 2 == 0 else 3 * n + 1
c += 1
print(c)What is printed?- A. 119
- B. 118
- C. 75
- D. 43
Correct answer: B. 118
Simulation takes 118 steps (75 halvings and 43 tripling steps). Counting the terms including 97 gives 119, while 75 and 43 count only one kind of step.
More Coding Interview Problems questions
- Problem: How many subsets of {1, 2, ..., 12} have an odd sum? Run the code. python print(sum(1 for m in range(1 << 12) if sum(i + 1 for i…
- Problem: What is the largest value of x such that x^3 <= 10^12 (integer cube root)? Run the binary search. python lo, hi = 0, 10 6 while…
- Problem: How many integers in [1, 1000] are divisible by 2 and 5 but not by 4? Run the code. python print(sum(1 for x in range(1, 1001) if…
- Problem: Which algorithm finds the convex hull of n points in O(n log n) by sorting and building lower and upper chains?
- Problem: What is the expected linear-time algorithm for the smallest enclosing circle of n points?
- Problem: Which data structure answers range-GCD queries with point updates in O(log n) each?