Happy Number
Updated
Problem statement
A positive integer is happy if repeatedly replacing it by the sum of the squares of its decimal digits eventually reaches 1. Otherwise the process repeats values forever.
Complete isHappy. Given the positive integer n, return true if its sequence reaches 1, and false otherwise. The initial value 1 is happy. Zero digits contribute zero to a transformation.
Function
isHappy(n: int) → booleanExamples
Example 1
n = 19return = trueThe sequence is 19→82→68→100→1.
Example 2
n = 2return = falseThe sequence enters the repeating 4→16→37→58→89→145→42→20→4 cycle and never reaches 1.
Example 3
n = 1return = trueThe starting value is already 1, so no transformation is needed.
Constraints
1 <= n <= 2^31 - 1.- Use the ordinary base
10digits ofn; no string formatting or leading zeroes are part of the input.
More Snowflake Phone Screen questions
- Design In-Memory File SystemPHONE SCREEN · Seen Nov 2026
- Merge Two Binary TreesPHONE SCREEN · Seen Nov 2026
- Connect Four from the Last MovePHONE SCREEN · Seen Nov 2026
- Minimum Edits to a Perfect Binary TreePHONE SCREEN · Seen Nov 2026
- Configurable Per-Client Token BucketsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Find Valid SET Card TriplesOA · PHONE SCREEN · Seen Nov 2026
- Minimum Index Distance Between Person and CakeOA · PHONE SCREEN · Seen Nov 2026
- Minimum N-ary Tree Depth DeletionsOA · PHONE SCREEN · Seen Nov 2026
- Sum of Binary Tree Boundary NodesOA · PHONE SCREEN · Seen Nov 2026
- Greedy Person-Cake Matching DistancePHONE SCREEN · Seen Nov 2026
Similar questions at other companies
- Merchant Payout Reserve CalculatorStripe · PHONE SCREENStripe interview questions
- Halfway Course in a Single TrackCrunchyroll · PHONE SCREENCrunchyroll interview questions
- Employee Count Median, Mean and ModeClay · PHONE SCREEN
- Unique Pairs in a 2D Matrix Summing to TargetAmazon · PHONE SCREEN
- Average Grades per StudentHartford Financial Services · PHONE SCREENHartford Financial Services interview questions
- Sparse Vector Dot ProductWhatNot · PHONE SCREENWhatNot interview questions
- Maximum Number of Balls in a BoxRoblox · PHONE SCREENRoblox interview questions
- Median of Parsed Integer StringsRippling · PHONE SCREENRippling interview questions
- First Node Two Linked Lists ShareAdobe · PHONE SCREENAdobe interview questions
- Roman Numeral to IntegerScale AI · PHONE SCREENScale AI interview questions