Skip to main content

28. Happy Number

easyAsked at Reddit

Determine if a number reaches 1 under iterated sum-of-squares of digits. Reddit uses this to test cycle detection on derived sequences — the same shape as detecting fingerprint hashes that loop in their bot-detection telemetry.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Reddit loops.

  • Glassdoor (2026-Q1)Reddit phone screen for warm-up on cycle-detection.

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, 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

Input
n = 19
Output
true

Explanation: 1^2+9^2=82, 8^2+2^2=68, 6^2+8^2=100, 1^2+0^2+0^2=1.

Example 2

Input
n = 2
Output
false

Approaches

1. Hash set of seen values

Track every value you've seen. If you see a repeat (and it's not 1), it's a cycle.

Time
O(log n) per step, ~unbounded steps
Space
O(k)
function isHappy(n) {
  const seen = new Set();
  const next = (n) => {
    let s = 0;
    while (n) { const d = n % 10; s += d * d; n = Math.floor(n / 10); }
    return s;
  };
  while (n !== 1 && !seen.has(n)) {
    seen.add(n);
    n = next(n);
  }
  return n === 1;
}

Tradeoff: Clear, easy. O(k) memory where k is cycle length.

2. Floyd's cycle detection (optimal)

Slow moves 1 step, fast moves 2 steps. If they meet at 1, happy. If they meet elsewhere, cycle.

Time
same
Space
O(1)
function isHappy(n) {
  const next = (n) => {
    let s = 0;
    while (n) { const d = n % 10; s += d * d; n = Math.floor(n / 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: Constant space. Same algorithm as cycle detection in a linked list.

Reddit-specific tips

Reddit interviewers grade on whether you recognize this is structurally cycle detection on a function-iteration. Bonus signal: mention that the values are bounded (for n < 2^31 the next value is at most ~10 * 81 = 810), so the cycle is small.

Common mistakes

  • Using a list instead of a set — O(n) lookups.
  • Forgetting that n === 1 is also a fixed point (Floyd's fast might equal 1 directly).
  • Doing string-based digit extraction — slower than mod 10.

Follow-up questions

An interviewer at Reddit may pivot to one of these next:

  • Find the duplicate number (LC 287) — same Floyd's on array indexing.
  • Linked List Cycle (LC 141).
  • What's the proof that all unhappy numbers cycle? (Trapping at small values via boundedness.)

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why is Floyd's preferred?

O(1) extra memory. Reddit interviewers often follow up with the cycle-detection lineage.

Are all numbers either happy or cycle to 4?

Yes — for small n there's a known cycle 4->16->37->58->89->145->42->20->4.

Practice these live with InterviewChamp.AI

Drill Happy Number and other Reddit interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →