Decide Whether Repeatedly Summing Squared Digits Reaches 1
Company: Snowflake
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: Starting from a positive integer, repeatedly replace it by the sum of the squares of its digits and decide whether the sequence reaches 1 or falls into a cycle. It tests cycle detection on an implicit sequence and reasoning about why the values stay bounded.
Constraints
- 1 <= n <= 2^31 - 1 (2147483647)
- n fits in a signed 32-bit integer (Java int, C++ int); no value produced by the process exceeds 2^31 - 1
Examples
Input: (7,)
Expected Output: True
Explanation: Source Example 1: 7 -> 49 -> 97 -> 130 -> 10 -> 1.
Input: (2,)
Expected Output: False
Explanation: Source Example 2: 2 -> 4 -> 16 -> 37 -> 58 -> 89 -> 145 -> 42 -> 20 -> 4 repeats without reaching 1.
Hints
- Write a helper that performs one step: take each decimal digit of the current number and add up the squares.
- The statement says a number that never reaches 1 eventually repeats; think about how you would notice that the sequence has come back to a value it already produced.
- After a single step even the largest allowed n becomes a small number, so the sequence only ever visits a small range of values.