Archive for July, 2015

How Do You Solve FizzBuzz?

Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.

This is one of the most well known interview questions for getting a development job. It’s meant to get rid of all those who lack skills in programming, and simple logic. You require programming skills to figure out a clean way to implement it, and logic skills to figure out how to implement it.

There have been many, many different ways to implement it in all different sorts of languages over time. Let’s look at some clever, and some not-so-clever, ways of doing it.

The Simple Solution

The totally understandable and language independent method with minor code duplication.
You can do it in C.

Or you can do it in Python.

Or really any other language out there. You can make it slightly worse by nesting the if statements.

The idea is simple and works. Easy to understand and has decent performance, but this is FizzBuzz, so performance probably isn’t a concern.

The Other Simple Solution

You can sacrifice a bit of performance and you will end up with a bit shorter version of the simple solution. This solution will look a bit different depending on the language it is written in, simply by the fact that string concatenation is done differently in different languages.

In Python:

In C:

 

And in Java:

This is one of the most common solutions I see, but it does use a the slightly higher overhead of concatenating strings.

The “No Math” Solution

This isn’t a problem that requires user input. The output is fixed. Why should we bother calculating anything?

Technically only three lines of code. That’s less than the simple solution! In this day and age, with the extra disk space everywhere, this could be a decent solution, but it lacks the simple logic that FizzBuzz is supposed to show.

The “What’s a Modulo?” Solution

Watch out, if somebody has never used the modulo operator, they’re probably not the most well-versed programmer. But it is a cool experiment.

You could always reset the fizz or buzz counters inside each if/elif statement, but readability would slightly go down.

The Lookup Table with Tuples Solution

This is very dependent on the language it is used in. But it’s one of my favorite ways to solve FizzBuzz. Here it is in Python.

This may not be the best for performance, or the simplest. But, it has an elegant look to it.

The Enterprise Solution

This one seems to come up a lot in discussions about FizzBuzz. It’s a satire of FizzBuzz and enterprise software. But, it does work.

Since this solution consists of 1704 lines of code at the moment, I don’t think I’ll put this on directly on the blog. You can find the its Github page here.

How Do You Solve FizzBuzz?

There are many more interesting solutions out there. Most of them are probably way over the top, or totally wrong. Finding a new way to solve it (or fail to solve it) is something I look forward too.

If you have a unique solution, or an improvement on the generic ones, share it!

Who Knew? Database Design!

I recently had to think about designing a database for the first time. Now, I have put up tables every-so-often with a few columes to store some things. And I did know how to throw together some Java or PHP so I could interact with those tables. But, now, for my first time ever, I had to design a database. The whole thing. The many tables and connections between them. I had to think about how everything interacts, and how I would modify the data and access it. And I loved this whole process.

What do you think a database is?

I never realized it before, but I had no idea what a database actually was. I don’t know about you, but I always though it was simply a few sets of tables with some columes and a few rows of data that go into it. Maybe scaled up to hold millions of rows of data, but the same idea. I used to think it was as simple as creating a new table with the columes set to what you needed.

After setting up that table (I don’t store passwords, don’t worry) I would just start pushing data into it. I didn’t really think of that as designing anything, or planning anything. Sure, I would think over what kind of data type each field is. And, of course, I would figure out what fields I needed. But I never really planned it out. That’s just what a database was to be.

So what is a database?

A database is a collection of data. A bunch of things that go with other things in certain ways. But that doesn’t help us when we get to the code of it. No. But I think that you need to spend a little more time thinking about your database design. It’s more than just a table of data spat out. It’s how it all relates. It’s how you think about it. And it’s what you plan on doing with it.

Instead of writing out some code, take some time and grab a piece of paper or a whiteboard, and write it all down. Then think about it. There is probably way better ways to do it than just starting with the code (as always).

Plus, if you want to put together a larger database with a bunch of different kinds of data, it may be time to actually design it. Grab that paper, or better yet, some software that’s meant for it. Next you need to think about it. Consider it. Put together all the kinds of things you’re storing and how they relate. Plan it out. Then, after you know exactly what you need and want, you can start writing your code.