Collatz sequence steps with memoized range maximum
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked at Airbnb. It tests memoization, simulation and math.
Starting from a positive integer n, repeat the following until n equals 1:
- If
nis even, setn = n / 2. - If
nis odd, setn = 3n + 1.
Each replacement counts as one step. (It is widely believed, though unproven, that every positive integer reaches 1.)
Part 1. Implement collatz_steps(n: int) -> int, which returns the number of steps needed to reach 1. collatz_steps(1) is 0. Raise an error for n < 1.
Part 2. Implement longest_up_to(n: int) -> tuple[int, int], which returns (start, steps) for the starting value in [1, n] that needs the most steps. If several starts tie, return the smallest. Your solution should handle n up to 1,000,000 in a few seconds, so avoid recomputing work that earlier starting values already did.
Example 1
collatz_steps(6) -> 8
# 6 -> 3 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1
Example 2
longest_up_to(10) -> (9, 19)
# 9 needs 19 steps; no start from 1 to 10 needs more.
Constraints
-
1 <= n <= 10^6for Part 2. - Intermediate values can grow far larger than
n(starting from 27, the sequence climbs past 9,000).
Clarifying questions
- Should Part 1 also use the cache built by Part 2?
- Is recursion allowed, given that some sequences take hundreds of steps?
- How much memory may the cache use: a dict of every value seen, or only values up to
n? - Should the function reject non-integers as well as
n < 1?
What a strong answer covers
- Part 1 is a loop that counts steps until 1 and raises for
n < 1. - Part 2 memoizes steps in an array of size
n + 1. For each start, it follows the sequence until it reaches a value below the start (whose count is already known), counts the steps taken, and fills in the values on the way that are within the array. - Keeps the walk iterative with a small list of visited values, so no recursion depth issues, and uses plain integers since values can grow past
n. - Tracks the maximum with a strict
>comparison so the smallest start wins a tie. - Tests: both examples,
n = 1((1, 0)), a known value such as27(111 steps), and a check of the memoized result against the plain loop for every start up to 1,000.
Follow-ups
- Answer
longest_up_tofor many differentn: precompute once and keep a running maximum. - Speed it up further: skip every start at or below
n / 2, since2xtakes one more step, or take several steps at once. - Return the highest value reached by any sequence up to
n. - Generalize the rule to
an + band detect sequences that loop instead of reaching 1.
Where it's asked
What it tests
memoization · simulation · math
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.
