Starting Value with the Longest Collatz Chain
Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The ZipRecruiter OA reported in April 2022 asks for the starting value up to a limit with the longest Collatz chain, and the whole thing hinges on one data structure: a cache of chain lengths. Limit goes up to 1,000,000, so recomputing every chain from scratch is the trap. It's dynamic programming with memoization, and it's simpler than it sounds. If your head goes blank when the timer starts, StealthCoder runs invisibly on your screen and can hand you the memoized approach as a safety net. Know the idea first, though, and the code writes itself.
The problem
For a positive value n, repeatedly replace an even value by n / 2 and an odd value by 3n + 1 until reaching 1. Among starting values from 1 through limit, return the one with the longest chain, counting both the start and 1. Break equal lengths by the smaller starting value. Function longestCollatzStart(limit: int) → int Examples Example 1 limit = 1 return = 1 Only starting value one is available. Example 2 limit = 2 return = 2 Two has a longer chain than one. Constraints 1 <= limit <= 1000000 Intermediate values fit in signed 64-bit integers.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that chains overlap. Once you've computed the length for some value, any later chain that lands on it can stop and add the stored length. Use an array or hash map keyed by value. For values under the limit, an array of size limit+1 is fast. For larger intermediate values, which can exceed the limit and need 64-bit integers, use a hash map or just skip caching them. Walk each chain iteratively until you hit a cached value or 1, then fill lengths back. Track the best start, and only replace it on a strictly longer length so ties keep the smaller start. Common pitfalls: recursion depth blowing up, int overflow on 3n+1, and forgetting that the chain length counts both the start and 1. If you freeze mid-OA, StealthCoder is the hedge that gives you a clean version fast.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Starting Value with the Longest Collatz Chain cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass ZipRecruiter's OA.
ZipRecruiter reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Starting Value with the Longest Collatz Chain FAQ
What's the trick to the ZipRecruiter longest Collatz chain problem?+
Memoize chain lengths. Chains share tails, so once a value's length is known, any chain reaching it stops early. Store lengths in an array for values up to limit. This turns a brute-force run into roughly linear amortized work over a million starts.
How hard is this problem really?+
Easy to medium. The Collatz rule is simple and the logic is short. The difficulty is performance at limit 1,000,000 and handling overflow. If you know memoization, it's about fifteen lines. Without caching, it can be painfully slow.
How do I handle ties between chain lengths?+
The problem says the smaller starting value wins. Iterate starts from 1 upward and update your best only when the new length is strictly greater than the current best. Equal lengths then never overwrite the earlier, smaller start.
Do I need 64-bit integers?+
Yes. The constraints say intermediate values fit in signed 64-bit, and 3n+1 can climb well past the limit. In languages with 32-bit ints by default, use long. Only cache values below limit in an array, and skip or hash the larger ones.
How do I prepare for this in 48 hours?+
Write the memoized Collatz solution from scratch twice. Practice the iterative version that walks until it hits a cached value, then backfills lengths. Test the edge cases limit = 1 and limit = 2, and check that ties and overflow behave correctly.