Happy Number
An easy Math interview guide. Task statement, worked examples, intuition, and step-by-step solutions.
- Topic
- Math
- Sheets
- 2
- Core for
- 13 roles
The problem
Take a positive number n, replace it with the sum of the squares of its digits, and keep repeating. Some numbers eventually hit 1 and stay there; these are called happy. Others fall into a loop that never reaches 1. Report whether n is happy.
Example 1
- Input
- n = 19
- Output
- true
- Why
- 19 -> 1^2+9^2=82 -> 8^2+2^2=68 -> 6^2+8^2=100 -> 1^2+0^2+0^2=1. The process reaches 1, so 19 is a happy number.
Example 2
- Input
- n = 2
- Output
- false
- Why
- 2 -> 4 -> 16 -> 37 -> 58 -> 89 -> 145 -> 42 -> 20 -> 4... enters a cycle that does not include 1, so 2 is not happy.
Constraints
- 1 <= n <= 2^31 - 1
How to think about it
Updated 2026-09-29The sum of squares of digits of any 32-bit integer quickly collapses into a bounded set below 243. Because the domain is finite and deterministic, the sequence must either reach 1 or enter a closed loop, making cycle detection the exact mathematical match.
Approaches, worst first
Hash set cycle detection
time O(log n) · space O(log n)
Compute the digit square sum iteratively, inserting each seen number into a hash set. If the value hits 1, it is happy; if it matches an element already in the set, a cycle is found.
Floyd cycle-finding algorithmWrite this one
time O(log n) · space O(1)
Advance two pointers: slow steps forward one digit-square sum at a time while fast steps twice. If fast reaches 1, the number is happy; if slow and fast collide before 1, it is stuck in a non-happy cycle, all without allocating a hash set.
Where people lose marks · 2
- Failing to terminate on numbers that loop indefinitely, causing an infinite loop when cycle detection is omitted.
- Incorrect digit extraction logic that misses the last remaining digit when stripping with n / 10.
The theory behind it
Math — the ground this problem stands on. All Math problems
What Math is
Algorithmic math is the application of number properties, modular arithmetic, and geometric patterns to compute results without simulating every step. Rather than filling memory buffers with millions of simulated counters or walking endless loops, mathematical formulation uses identities like prime factorization, digit extraction, and greatest common divisors to jump directly to target answers through closed-form rules.
When to reach for it
Reach for mathematical techniques when problem bounds are massive, such as constraints reaching ten to the eighteenth power where simulation is impossible. Strong prompts mention prime generation, greatest common divisor calculations, modular inverses, fast exponentiation, digit reversals, or combinatorial arrangements. If brute-force counting produces memory overflow or time limit exceeded warnings, algebraic reorganization is the intended path.
How the pattern works
Focus on numeric constraints and potential overflows before writing expressions. Break numbers into constituent digits using modulo ten to peel least significant figures and integer division to shrink scale. For divisibility, apply Euclid's remainder theorem repeatedly until remainder drops to zero. Apply modulo arithmetic at every intermediate addition and multiplication stage rather than once at the end, preventing values from spilling beyond standard numeric register limits.
More Math problems
Target Roles
Core requirement for 13 roles:
Track in your role's order
Pick your target role and all 370 problems resequence to what that interview actually asks.
Start freeProblem set and role mapping as of .