I got 97 (coding) problems, but these 2 ain’t one
A look at solving the Collatz and FizzBuzz coding problems
As someone about to embark on the interview process, I’ve begun to start working on coding challenges and decided to start with the famed “Collatz” and “FizzBuzz” problems. Below are my solutions and the logic I implemented in solving them.
Collatz
The “Collatz conjecture” can be found on Project Euler. This problem states as follows:

My initial reaction was to break this problem down, piece by piece. I first wrote a method called num that had a conditional to determine whether a number was even or odd. Based on the given number, it would execute the code accordingly (ie- n/2 for an even number, 3n + 1 for an odd number). It quickly became evident I’d need an object to call this method on, so I put this method in a Class called Number, provided it with an initialize method, and gave it the appropriate getter methods.
Important Realization
I realized that a central part of this problem is not the actual numbers, but counting the length of the longest chain of numbers. Hence, I created a while loop in my num method that would shovel every number in a given chain into an array called new_num. After doing this, I placed the array outside the class and called .count on it to see how many numbers were in the array.
After finishing this class, I wanted to put it into action. I created two variables called starting_number and longest_chain outside of the class and set them to 1 and 0 respectively (this will come in handy later). Then, I needed to go through all the numbers between 1 & 1,000,000. I decided to call the .upto method and then passed it a block that would iterate through each of these million numbers. As the block began, I instantiated a new number and stored it in variable called number_object. I then called the num method on number_object, and stored this into a variable called array_count. Finally, I referred back to the starting_number and longest_chain variables that I created earlier, and set the starting_number to whichever number would be passed in during a particular instance, and set longest_chain equal to the array_count. However in order to save the largest number count, I used a conditional that the array_count SHOULD ONLY be kept if it’s larger than the longest_chain.
The Collatz solution & the actual code
The solution to which number that has the largest chain under 1,000,000 is 837,799, with a chain that’s 525 numbers long. Here’s the code in it’s entirety as described above.
FizzBuzz
Then I decided to take a look at the classic FizzBuzz question. The FizzBuzz question is as follows:

FizzBuzz is an important question because it’s designed to show that you, know how to code- but is also can also show to see an engineer’s shrewdness, as there’s a potential lurking “gotcha.”
Solution
Here’s my FizzBuzz code:
I think the reason why FizzBuzz is such a popular question is that there are tons of ways to solve it. In fact, I remembered that Flatiron had assigned us a FizzBuzz lab and decided to check the results to see if any of my classmates had solved it differently. Lo and behold, the first three solutions I checked were all different. One person solved it using a loop & an array, another with a case-when statement, and another through using a while loop!
Main FizzBuzz Takeaway
The FizzBuzz test illustrates an important lesson in programming. Namely, when presented with a new problem, it’s important to try and solve it with your existing knowledge and through using the tools you’re most comfortable with.
Now about those 97 other problems… ☺