27. Happy Number
easyAsked at SnowflakeDetermine whether repeatedly summing the squares of a number's digits eventually reaches 1. Snowflake uses this to test cycle detection in a function-iteration setting — relevant for recursive CTE termination analysis.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Snowflake loops.
- Glassdoor (2025-Q4)— Snowflake compiler-team uses this to set up recursive-CTE termination discussion.
- LeetCode Discuss (2025-08)— Reported at Snowflake new-grad screens.
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, and repeat the process until the number equals 1, or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy.
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. Visited set
Iterate, storing every seen value. Return true if you hit 1; false if you revisit.
- Time
- O(log n)
- Space
- O(log n)
function isHappy(n) {
const seen = new Set();
function nextNum(x) {
let s = 0;
while (x > 0) {
const d = x % 10;
s += d * d;
x = Math.floor(x / 10);
}
return s;
}
while (n !== 1 && !seen.has(n)) {
seen.add(n);
n = nextNum(n);
}
return n === 1;
}Tradeoff: Works, O(log n) space.
2. Floyd's tortoise and hare (optimal)
Apply the function once vs twice; if there's a cycle, they meet; if the meeting point is 1, happy.
- Time
- O(log n)
- Space
- O(1)
function isHappy(n) {
function nextNum(x) {
let s = 0;
while (x > 0) {
const d = x % 10;
s += d * d;
x = Math.floor(x / 10);
}
return s;
}
let slow = n, fast = nextNum(n);
while (fast !== 1 && slow !== fast) {
slow = nextNum(slow);
fast = nextNum(nextNum(fast));
}
return fast === 1;
}Tradeoff: O(1) space, applying the cycle-detection pattern to function iteration instead of pointer iteration.
Snowflake-specific tips
Snowflake interviewers will note whether you reuse the Floyd's-tortoise-and-hare pattern from linked lists. Bonus signal: discuss recursive CTE termination — Snowflake (and the SQL standard) requires recursive CTEs to converge; this problem is the same convergence/divergence dichotomy.
Common mistakes
- Computing digit-square-sum with parseFloat or string parsing — overkill.
- Storing the entire trace in a list instead of a Set — O(n^2) lookup.
- Forgetting that n = 1 must short-circuit before any computation.
Follow-up questions
An interviewer at Snowflake may pivot to one of these next:
- Detect cycles in linked-list-style data structures.
- How would you generate ALL happy numbers up to N?
- Recursive CTE termination guarantees in Snowflake SQL.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why does it eventually cycle or hit 1?
The digit-square-sum function is bounded (for any 3-digit number, max is 9^2 * 3 = 243), so the sequence is confined to a finite set. By pigeonhole, it must either reach 1 or revisit a previous value.
How does this relate to recursive CTE?
A recursive CTE that doesn't converge would run forever. Snowflake has a max iteration limit and tools like SEED ROW + UNION ALL TO mirror exactly the kind of convergence reasoning Happy Number tests.
Practice these live with InterviewChamp.AI
Drill Happy Number and other Snowflake interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →