27. Happy Number
easyAsked at SalesforceDetermine if a number is 'happy' — repeatedly replace with sum of squares of its digits until it reaches 1 (happy) or cycles forever. Salesforce uses this to test cycle detection in a synthesized sequence.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Salesforce loops.
- Glassdoor (2026-Q1)— Salesforce sometimes asks this as a 'spot the Floyd cycle' question outside lists.
- LeetCode Discuss (2025)— Used as a recurrence-cycle detection problem.
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 (happy), or it loops endlessly in a cycle which does not include 1. Return true if n is a happy number, otherwise false.
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 + 0 = 1.
Example 2
n = 2falseApproaches
1. Set-based cycle detection
Apply the transformation; store each result in a set. If you revisit, you're in a cycle.
- Time
- O(log n) per step, O(?) steps
- Space
- O(steps)
function isHappy(n) {
const seen = new Set();
const next = (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 = next(n);
}
return n === 1;
}Tradeoff: O(steps) extra space. Salesforce will ask if you can do O(1).
2. Floyd's tortoise and hare
Treat next() as a transition function. Slow advances 1; fast advances 2. They meet inside any cycle.
- Time
- O(?)
- Space
- O(1)
function isHappy(n) {
const next = (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 = next(n);
while (fast !== 1 && slow !== fast) {
slow = next(slow);
fast = next(next(fast));
}
return fast === 1;
}Tradeoff: O(1) space. Reusing the cycle-detection algorithm from linked lists shows pattern recognition.
Salesforce-specific tips
Salesforce uses this to see if you can apply Floyd's algorithm OUTSIDE the linked-list context. Bonus signal: argue that the sequence is bounded (sum of squares of digits is < 9^2 * digits, which shrinks fast), so the trajectory must eventually cycle or hit 1.
Common mistakes
- Not checking n !== 1 termination — infinite loop on happy numbers.
- Using a set in interview but not realizing Floyd's is the expected O(1) follow-up.
- Computing digits via toString — slower than mod/divide for large n.
Follow-up questions
An interviewer at Salesforce may pivot to one of these next:
- Detect cycle in linked list (LC 141) — exact same algorithm shape.
- Find the start of the cycle (extends LC 142 logic).
- Prove that any unhappy number's cycle includes 4.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
How is this related to linked-list cycle detection?
next() acts as a successor function. The sequence n -> next(n) -> next(next(n)) is a 'list' where Floyd's algorithm detects whether we cycle or reach the terminal state 1.
Why does the sequence always cycle or hit 1?
For n > 1000, next(n) < n (sum of squared digits shrinks). For n <= 1000, there are finitely many values, so the sequence either reaches 1 or cycles.
Practice these live with InterviewChamp.AI
Drill Happy Number and other Salesforce interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →