To learn more about the newsletter, check our detailed About Page + FAQs
To help me understand you better, please fill out this anonymous, 2-min survey. If you liked this post, make sure you hit the heart icon in this email.
Recommend this publication to Substack over here
I’m sure you couldn’t wait for this,
One of my subs was asked this question in their IBM software engineering interview. I thought about this question for quite a while, trying and testing different variants.
This problem can be found as problem 202. Happy Number on Leetcode.
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 (where it will stay), 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.
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
Constraints:
1 <= n <= 231 - 1
You can test your solution here
[FREE SUBS]To buy the solution for this problem, use any of the following links. The price is 3 USD. Once you send the payment, message me on IG, LinkedIn, Twitter, or by Email:
Venmo: https:/ount.venmo.com/u/FNU-Devansh
Paypal: paypal.me/ISeeThings
Or you can sign up for a Free Trial Below. Since the reception has been great, I got a special offer for y’all. You can get this newsletter free for 2 months, just by using the button below-
Step 1: Analyzing the Definitions
In such cases, I believe it is best to attack by trying to break down the definitions given in the problem. By clearly defining the problem and all the definitions, we can gain a lot of insight into the problem. And in many cases, it can help you come up with mathematical rules and formulations that will let you solve the problem much quicker than average. This might just be my Math Degree, and work in AI, but this approach is definitely a competitive advantage. There aren’t many software people that actively try to look for patterns or mathematical/algorithmic statements. Even when you can’t come up with complete functions to generate the output, you will gain some insights into how to solve/optimize this problem.
I like to go bottom-up when attacking definitions, going with the simplest first. Regular readers know how powerful going for low-hanging fruit is, especially in contexts like the interview where you’re expected to communicate your thoughts in a coherent manner. So let’s attack the smallest definition we can see- what does it mean for a number to loop endlessly?
This might seem obvious to y’all, but take a step back. How would you define an infinite loop? Some numbers might just take a very very very long time to converge to happiness, but they aren’t unhappy. Calling them unhappy would be wrong, and would probably hurt their feelings.
So let’s wrack our brains to see if we can come up with something. Think back to how you can tell if we are stuck in a loop. What definitions are used commonly?
Keep reading with a 7-day free trial
Subscribe to Technology Made Simple to keep reading this post and get 7 days of free access to the full post archives.