27. Happy Number
easyAsked at VercelDetermine whether a number is 'happy' (repeatedly replace with sum of squares of digits until it reaches 1, or loops forever). Vercel asks this for the cycle-detection variant of Floyd's algorithm — same shape as detecting redirect loops in their edge router.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Vercel loops.
- Glassdoor (2025-11)— Vercel screen; cycle detection bonus signal.
- Blind (2026-Q1)— Listed in Vercel runtime screen pool.
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. Hash set to detect cycle
Track every number seen. If we revisit one, we're in a loop.
- Time
- O(log n) per step, polylog total
- Space
- O(polylog)
function isHappy(n) {
const seen = new Set();
while (n !== 1 && !seen.has(n)) {
seen.add(n);
let next = 0;
while (n > 0) {
const d = n % 10;
next += d * d;
n = Math.floor(n / 10);
}
n = next;
}
return n === 1;
}Tradeoff: Uses a Set. Vercel may ask for O(1) space via Floyd's.
2. Floyd's cycle detection (optimal space)
Slow and fast pointers in the sum-of-squares sequence. If they meet at 1, happy; if they meet elsewhere, cycle.
- Time
- O(polylog)
- Space
- O(1)
function isHappy(n) {
const sq = (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 = sq(n);
while (fast !== 1 && slow !== fast) {
slow = sq(slow);
fast = sq(sq(fast));
}
return fast === 1;
}Tradeoff: O(1) extra space. The 'sum of squares' function is the iteration; we apply Floyd's the same way as on a linked list.
Vercel-specific tips
Vercel grades whether you recognize this as cycle detection. The hash-set version is fine; the Floyd's version is the bonus. Bonus signal: explaining that any sequence on a finite state space eventually cycles, so we only need to detect either '1' (happy) or 'any other repeat' (sad).
Common mistakes
- Recursing without a cycle guard — infinite recursion on sad inputs.
- Forgetting the n === 1 exit — keeps looping at 1 (1^2 = 1).
- Computing the digit sum with a string conversion in a hot loop — slow.
Follow-up questions
An interviewer at Vercel may pivot to one of these next:
- Find all happy numbers in [1, N].
- Floyd's for general cycle detection in a functional graph.
- Linked List Cycle (LC 141) — same algorithm, different domain.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why does the sequence always either hit 1 or cycle?
The sum of squares of digits of any number eventually falls below 1000 (because the function grows slower than n). On a finite state space, every infinite walk eventually revisits a state.
Is the hash-set version 'wrong'?
No — it's correct and clearer. Floyd's is just a space optimization. Vercel will accept either, but mentioning Floyd's signals depth.
Practice these live with InterviewChamp.AI
Drill Happy Number and other Vercel interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →