call(back)

Scale Digit-Square Convergence Requests

A service answers one question: given a positive integer n, repeatedly replace it with the sum of the squares of its decimal digits — does the sequence eventually reach 1? (This is the happy-number test.)

Design the service for very high volume: billions of requests over time, low latency per request, and no expensive repeated computation for common inputs. You are given an observation to exploit: after a small number of transformations, any fixed-width integer collapses into a small numeric range.

Cover: the per-request algorithm and its cost; what you precompute and how big it is; whether and where caching helps; the API, including batching; capacity planning; input validation; deployment and latency; and how you would test and monitor it.

Hints

Hint 1

Bound the first step: a d-digit number maps to at most 81·d. For a 64-bit integer that is at most 1,620 — so after one pass every input lands in a range you can tabulate completely at start-up.

Hint 2

Once the answer is one digit pass plus an array lookup, the computation costs tens of nanoseconds. Ask what a network hop to a shared cache costs by comparison.

Hint 3

When compute is free, the request itself is the expense: think batching, connection reuse, immutable responses cached at the edge, or shipping the table to the caller.

Asked at