I remember reading a passage on Quora a few years ago that really stuck with me. I’ve lost touch with the exact source, but from what I remember, the author was in the role of interviewing/hiring developers. The author’s basic idea was that experienced developer’s wrote smaller functions. Of course, it wasn’t until much later that I discovered the hidden benefit of writing smaller functions.
Hi Everyone! I was working on a FreeCodeCamp algorithm challenge the other day and kept running into timeout issues when I ran it in their editor. I had tried it elsewhere and it was working, but I figured that I needed to optimize the solution so that it would run on FreeCodeCamp’s servers. After spending quite a bit of time working on how to get the smallest common multiple, I think I’ve arrived at a really performant solution, so I wanted to share it with you all here.
If you’d like to try it out for yourself, the challenge is called Smallest Common Multiple on FreeCodeCamp’s website. The instructions say that we’re going to have a function called
smallestCommons that will take a single parameter
arr. Our parameter
arr is an array with two elements in it (
[1,5]) and we’re told that they may not be in numerical order. The task we’re given is to find the smallest common multiple of the provided numbers (in
arr) that can be evenly divided by both, as well as by all of the sequential numbers in the range between the two numbers.
For example, for
smallestCommons([1,5]) we’d need to return the smallest number that is evenly divisible by 1, 2, 3, 4 & 5. The test cases we’re given are as follows:
smallestCommons([1,5]) should return a number. smallestCommons([1,5]) should return 60. smallestCommons([5,1]) should return 60. smallestCommons([1,13]) should return 360360. smallestCommons([23,18]) should return 6056820.