28. Happy Number
easyAsked at WorkdayDetermine if a number is 'happy' — iterating the sum-of-digits-squared reaches 1. Workday uses this to test cycle-detection on numeric sequences — same Floyd's pattern as in linked lists.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Workday loops.
- Glassdoor (2025)— Workday SDE1 phone screen.
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 of seen values
Iterate; if we see a repeat, cycle. If we see 1, happy.
- Time
- O(log n) per step
- Space
- O(log n)
const seen = new Set();
while (n !== 1 && !seen.has(n)) { seen.add(n); n = sumSq(n); }
return n === 1;Tradeoff: Easy and correct, but O(k) extra space for the set.
2. Floyd's cycle detection
Slow steps once, fast steps twice. If fast reaches 1, happy. If they meet, cycle.
- 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) extra space. Same Floyd's idea as linked-list cycle detection, just on a numeric sequence.
Workday-specific tips
Workday loves connecting this to Linked List Cycle — both are Floyd's on different graph representations. Articulating that connection earns 'pattern recognition' credit. Don't forget the early-exit when fast hits 1.
Common mistakes
- Forgetting to compute sumSq when x is 0 — single iteration returns 0, not handled by next().
- Using only fast === 1 as exit — misses the cycle case.
- Implementing sumSq with string conversion — slower than digit math.
Follow-up questions
An interviewer at Workday may pivot to one of these next:
- Linked List Cycle (LC 141) — same pattern.
- Sad numbers — all non-happy numbers eventually enter the cycle containing 4.
- What if 'happy' is generalized to other base reductions?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why does Floyd's work here?
next() is a deterministic function — every input has exactly one output. That makes the sequence a functional graph, and any infinite walk in such a graph must hit a cycle.
Why doesn't the sequence grow unboundedly?
For n with k digits, next(n) is at most 81*k. Once n shrinks below ~1000 it stays bounded — so the walk is on a finite state space and must cycle.
Practice these live with InterviewChamp.AI
Drill Happy Number and other Workday interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →