educative.io

How is the hash table method a naive approach?

It seems to me the hash table method will always find cycles faster . If the largest number is assumed to be about 2^63 - 1 (ie. ~9x10^18), which is 19 digits, then the largest sum of squares will be no more than 4-digits. You can make a perfect hash table (0 collisions) with 9,999 spaces just to be safe. There will never be any probing necessary as values can be looked up in O(1). However, I do see that this uses more space than needed, but the naive approach section makes it seem the main problem is slower time complexity.


Course: Grokking Coding Interview Patterns in Python - Learn Interactively
Lesson: Solution: Happy Number

Hello @Amjad_Ali,

Thank you for taking time and sharing the calculations with us. Indeed the hash table method is a good approach towards solving the problem but just like you mentioned, the choice of words need to be improved to avoid making it a “culprit” that the space complexity is the main issue towards not implementing the hash table method.

We’ll update the content and let you know once the updated version is live. Thanks!

Happy learning!