26. Happy Number
easyAsked at PlaidDetermine whether a number eventually reaches 1 under repeated digit-square-sum, or cycles forever. Plaid asks this because cycle detection on transformation sequences is the same algorithm as detecting OAuth refresh-token loops.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Plaid loops.
- Glassdoor (2025)— Plaid OA — disguised cycle detection.
- LeetCode Discuss (2026)— Plaid intro.
Problem
Write an algorithm to determine if a number n is happy. A happy number is defined by the following process: starting with any positive integer, replace the number by the sum of the squares of its digits. Repeat until the number equals 1, or it loops endlessly in a cycle which does not include 1. Return true if n is a happy number, false otherwise.
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
Apply the transform; if we see a repeat, we're in a cycle that doesn't include 1.
- Time
- O(log n) per step, bounded total steps
- Space
- O(k) where k is cycle length
function isHappy(n) {
const seen = new Set();
while (n !== 1 && !seen.has(n)) {
seen.add(n);
let next = 0;
while (n > 0) { next += (n % 10) ** 2; n = Math.floor(n / 10); }
n = next;
}
return n === 1;
}Tradeoff: Clear, correct. The space is bounded because digit-sum-squared monotonically shrinks large numbers.
2. Floyd's tortoise and hare
Treat the transform as a function on numbers. Slow steps once, fast steps twice. They meet at 1 (happy) or inside a cycle (sad).
- Time
- O(log n) per step, bounded
- Space
- O(1)
function isHappy(n) {
function sq(x) {
let s = 0;
while (x > 0) { s += (x % 10) ** 2; 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: Constant space, same logic as linked-list cycle. Demonstrates you spot the cycle-detection pattern outside its usual setting.
Plaid-specific tips
Plaid asks this to verify you spot disguised cycle detection. Bonus signal: explicitly identify the transform as 'a function we keep applying' — the same shape as iterating OAuth refresh tokens until a stable token is reached or a cycle is detected.
Common mistakes
- Returning true on any visit (forgetting to check for 1 specifically).
- Putting Set.has check after adding — never terminates.
- Using >> 0 to truncate instead of Math.floor — works for small numbers but obscures intent.
Follow-up questions
An interviewer at Plaid may pivot to one of these next:
- Identify the cycle length when the number is sad.
- Generalize to other digit transforms (cube, base-k).
- Detect cycles in an OAuth refresh sequence.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why does the process terminate?
For n > 999, sum-of-digit-squares is smaller than n. So we eventually drop below 1000 where there are only finitely many states — either 1 is reached or we cycle.
When is Floyd cleaner than a Set?
Floyd uses constant space. For a single call it's overkill; for batched calls on millions of numbers, the space matters.
Practice these live with InterviewChamp.AI
Drill Happy Number and other Plaid interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →