27. Happy Number
easyAsked at DatadogDetermine if a number is 'happy' — repeated sum of squares of digits eventually reaches 1. Datadog asks this as a cycle-detection-on-implicit-graphs question, where Floyd's algorithm applies even without a literal linked list.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Datadog loops.
- Glassdoor (2026-Q1)— Datadog interview — graded on cycle detection.
Problem
Write an algorithm to determine if a number n is happy. A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits. Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy. Return true if n is a happy number, and false if not.
Constraints
1 <= n <= 2^31 - 1
Examples
Example 1
n = 19trueExplanation: 1^2 + 9^2 = 82; 8^2 + 2^2 = 68; 6^2 + 8^2 = 100; 1^2 + 0^2 + 0^2 = 1.
Example 2
n = 2falseApproaches
1. Hashset of visited values
Iterate the transformation; remember every value seen. If you revisit, it's a cycle.
- Time
- O(log n)
- Space
- O(log n)
function isHappy(n) {
const seen = new Set();
while (n !== 1 && !seen.has(n)) {
seen.add(n);
let sum = 0;
while (n > 0) {
const d = n % 10;
sum += d * d;
n = Math.floor(n / 10);
}
n = sum;
}
return n === 1;
}Tradeoff: Linear-ish space. Correct but Datadog will push for O(1).
2. Floyd's tortoise and hare (optimal)
Treat the transformation as a function. Slow steps once, fast steps twice. If they meet, cycle; if either hits 1, happy.
- Time
- O(log n)
- Space
- O(1)
function isHappy(n) {
function next(x) {
let sum = 0;
while (x > 0) {
const d = x % 10;
sum += d * d;
x = Math.floor(x / 10);
}
return sum;
}
let slow = n, fast = next(n);
while (fast !== 1 && slow !== fast) {
slow = next(slow);
fast = next(next(fast));
}
return fast === 1;
}Tradeoff: O(1) memory — the cycle-detection pattern from linked-list problems applied to an implicit graph. Datadog-canonical.
Datadog-specific tips
Datadog grades on whether you recognize this as a cycle-detection problem on an implicit graph. The Floyd's tortoise-and-hare connection earns serious points; reciting it as a hashset solution misses the punchline.
Common mistakes
- Hardcoding 1, 4, 16, 20, 37, 58, 89 as the known cycle — works but signals you memorized rather than derived.
- Using BigInt unnecessarily — the sums stay well within 32-bit range.
- Off-by-one in the slow/fast initialization — they shouldn't start equal.
Follow-up questions
An interviewer at Datadog may pivot to one of these next:
- Linked List Cycle (LC 141) — same algorithm, explicit linked list.
- Linked List Cycle II (LC 142) — find the cycle start.
- Datadog-style: detect cycles in a metric-dependency graph in O(1) memory.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why does Floyd's work on a function instead of a linked list?
Any deterministic function on a finite domain eventually enters a cycle (pigeonhole). The transformation x -> sum-of-squared-digits is one such function.
Is the cycle space really bounded?
Yes — for inputs up to 2^31, the sum-of-squared-digits result is bounded by 9^2 * 10 = 810. After one step we're in a small state space, so cycles are short.
Practice these live with InterviewChamp.AI
Drill Happy Number and other Datadog interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →