### How to solved Fizzbuzz programming

## Thursday, March 11, 2010

## Credit : http://www.geekschool.org/programming/fizzbuzz/

## Get Hired With FizzBuzz

Let’s start Geek School off with a trivial problem that may come up in your next interview. The problem is based on a real drinking game. There are several variations but here’s the version we’ll work with:

Because programmers are usually more than a little obsessed with details, when asked to solve FizzBuzz a typical programmer will probably ask what language to use, or worry about how to run their code to show that it works (or doesn’t). That’s not important, though code that actually

Let’s start by making sure we understand the problem, then come up with a strategy for solving it. This is how programmers are supposed to solve problems, and this is the process the interviewer wants to see (assuming they aren’t just sadistic).

Watch for

Some languages require variables be declared before you use them, and some languages don’t. You should know how to declare variables and all of the basic types. For looping from 1 to 100 an integer will do. No need to get fancy with bytes or unsigned types unless you want to get quizzed about it. Counting loops should use integers, not floating (real) numbers, unless you are very sure what you’re doing. In a job interview you should expect to be asked about basic types of the language you are using, including the range of values each type can express. The FizzBuzz problem may look dumb but it opens the door for asking questions about things a programmer is expected to know without looking them up.

Here’s a pseudo-code version, which may satisfy some interviewers:

Remember your grade school arithmetic:

Fleshing out the pseudo-code:

Look over the pseudo-code for problems. The code as it is matches the initial description of the problem, but it has an error. What will happen when

You can fix this by changing the order of the tests:

Here are some final examples in various languages:

There are lots of other ways to write this code. It’s best to stick with the obvious — if you go for the clever solution you are more likely to make a mistake. In an interview situation be careful not to overestimate the interviewer: if you write too-clever code they don’t understand they might be put off and decide you’re too much of a hacker.

As dumb as it looked at first, FizzBuzz actually tested for some fundamental programming skills and familiarity with the language you’re writing in:

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 programming problem has been discussed a lot lately because apparently a lot of programmers can’t do it. I’ve never been asked this question in an interview; I had never heard of it until I read

**Why Can’t Programmers.. Program?**I have been asked to write code on a whiteboard during an interview, and it can be nerve-wracking even if you know what you’re doing. More than once I’ve made mistakes or had to wing it because the interviewer assumed I would have the language spec and all libraries and APIs memorized. FizzBuzz can be solved in any language without having to know any tricks or remember obscure library functions.Because programmers are usually more than a little obsessed with details, when asked to solve FizzBuzz a typical programmer will probably ask what language to use, or worry about how to run their code to show that it works (or doesn’t). That’s not important, though code that actually

*runs*would probably score some points for the candidate.Let’s start by making sure we understand the problem, then come up with a strategy for solving it. This is how programmers are supposed to solve problems, and this is the process the interviewer wants to see (assuming they aren’t just sadistic).

- print the numbers from 1 to 100
- for multiples of 3 print “Fizz” instead of the number
- for multiples of 5 print “Buzz” instead of the number
- for multiples of 3 and 5 print “FizzBuzz” instead of the number

`for`

statement to loop, something like: `for (i = 1; i <= 100; i++) {...}`

. Python is a little different: `for i in range(1, 101)`

. Ruby has a few ways to do this, but let’s start with `for i in 1..100`

. Counting loops like this are basic stuff, so you should know how to write this kind of loop in every language on your résumé, and especially the language the job interview will focus on.Watch for

**off-by-one**errors. A likely off-by-one error would count to 99 instead of 100. In a C-like language an off-by-one error looks like`for (i = 1; i < 100; i++)`

. In Python it’s easy to forget that the `range`

function returns a list from the first number up to but not including the last value, so `range(1, 100)`

returns the list `[1, 2, 3, ..., 98, 99]`

.Some languages require variables be declared before you use them, and some languages don’t. You should know how to declare variables and all of the basic types. For looping from 1 to 100 an integer will do. No need to get fancy with bytes or unsigned types unless you want to get quizzed about it. Counting loops should use integers, not floating (real) numbers, unless you are very sure what you’re doing. In a job interview you should expect to be asked about basic types of the language you are using, including the range of values each type can express. The FizzBuzz problem may look dumb but it opens the door for asking questions about things a programmer is expected to know without looking them up.

Here’s a pseudo-code version, which may satisfy some interviewers:

int i; for (i = 1; i <= 100; i++) { if i is a multiple of 3 print "Fizz" else if i is a multiple of 5 print "Buzz" else if i is a multiple of 3 and a multiple of 5 print "FizzBuzz" else print i }Obviously we’ll need to know how to find out if a number is a multiple of 3, 5, or both. If you don’t know how to do this you’re not going to get the job.

Remember your grade school arithmetic:

*y*is a multiple of*x*if*y/x*is a whole number. Another way to say that is*y*is a multiple of*x*if the remainder of*y/x*equals 0. The usual way to get the remainder of dividing one integer by another is the*modulo*operator or function. In C, C++, PHP, Java, JavaScript, etc. the modulo operator is`%`

, so `10 % 3`

equals 1 and `10 % 5`

equals 0.Fleshing out the pseudo-code:

int i; for (i = 1; i <= 100; i++) { if (i % 3 == 0) print "Fizz" else if (i % 5 == 0) print "Buzz" else if (i % 3 == 0 && i % 5 == 0) print "FizzBuzz" else print i }You’re going to need to know how to print a string and a number out. That varies by language:

`printf()`

in C and C++; `print`

in PHP, Python, and Ruby; `System.out.print`

in Java. If you chose C you’ll have to know about `printf()`

’s formatting characters: `printf("%d", i)`

. If you chose C++ you can use `printf()`

but you will probably be expected to know how to use `cout`

and output streams.Look over the pseudo-code for problems. The code as it is matches the initial description of the problem, but it has an error. What will happen when

`i`

equals 15? `15 % 3 == 0`

will be true, so the code will print Fizz when it should print FizzBuzz. The order of the tests (the if/else if statements) is significant: if the tests aren’t done in the right order the program will be wrong.You can fix this by changing the order of the tests:

int i; for (i = 1; i <= 100; i++) { if (i % 3 == 0 && i % 5 == 0) print "FizzBuzz" else if (i % 3 == 0) print "Fizz" else if (i % 5 == 0) print "Buzz" else print i }Testing if

`i`

is a multiple of 3 and 5 can be changed to testing if `i`

is a multiple of 15, because 15 is the least common multiple of 3 and 5. Any number that is a multiple of 3 and 5 will also be a multiple of the LCM of 3 and 5. The LCM in this case is 3 times 5, but if the problem involved multiples of 4 and 6 the LCM would be 12, not 24.Here are some final examples in various languages:

**C**#includeint main() { int i; for (i = 0; i <= 100; i++) { if (i % 15 == 0) printf("FizzBuzz"); else if (i % 3 == 0) printf("Fizz"); else if (i % 5 == 0) printf("Buzz"); else printf("%d", i); printf("\n"); } }

**C++**#includeusing namespace std; int main() { for (int i = 0; i <= 100; i++) { if (i % 15 == 0) cout << "FizzBuzz"; else if (i % 3 == 0) cout << "Fizz"; else if (i % 5 == 0) cout << "Buzz"; else cout << i; cout << endl; } }

**Java**public class FizzBuzz { public static void main(String [] args) { for (int i = 1; i <= 100; i++) { if (i % 15 == 0) System.out.print("FizzBuzz"); else if (i % 3 == 0) System.out.print("Fizz"); else if (i % 5 == 0) System.out.print("Buzz"); else System.out.print(i); System.out.println(); } } }

**Perl**#!/usr/bin/perl for ($i = 1; $i <= 100; $i++) { if ($i % 15 == 0) { print "FizzBuzz" } elsif ($i % 3 == 0) { print "Fizz"; } elsif ($i % 5 == 0) { print "Buzz"; } else { print $i; } print "\n"; }

**Python**for i in range(1, 101): if i % 15 == 0: print 'FizzBuzz' elif i % 3 == 0: print 'Fizz' elif i % 5 == 0: print 'Buzz' else: print i

**Javascript (execute in Firebug debugger)**for (var i=1; i <= 100; i++) { if (i % 15 == 0) console.log("FizzBuzz"); else if (i % 3 == 0) console.log("Fizz"); else if (i % 5 == 0) console.log("Buzz"); else console.log(i); }

**PHP**for ($i = 1; $i <= 100; $i++) { if ($i % 15 == 0) print "FizzBuzz"; elseif ($i % 3 == 0) print "Fizz"; elseif ($i % 5 == 0) print "Buzz"; else print $i; print "\n"; }

**Ruby**for i in 1..100 if i % 15 == 0 then print "FizzBuzz" elsif i % 3 == 0 then print "Fizz" elsif i % 5 == 0 then print "Buzz" else print i end print "\n" end

**LOLCode**HAI CAN HAS STDIO? I HAS A VAR IZ 0 IM IN YR LOOP UPZ VAR!!1 IZ VAR BIGR THAN 100? GTFO. KTHX IZ VAR LEFTOVAR 15 LIEK 0? VISIBLE "FIZZBUZZ" KTHX ORLY? IZ VAR LEFTOVAR 5 LIEK 0? VISIBLE "BUZZ" KTHX ORLY? IZ VAR LEFTOVAR 3 LIEK 0? VISIBLE "FIZZ" KTHX NOWAI VISIBLE VAR KTHX KTHX KTHXBYE

*Execute it here.*There are lots of other ways to write this code. It’s best to stick with the obvious — if you go for the clever solution you are more likely to make a mistake. In an interview situation be careful not to overestimate the interviewer: if you write too-clever code they don’t understand they might be put off and decide you’re too much of a hacker.

As dumb as it looked at first, FizzBuzz actually tested for some fundamental programming skills and familiarity with the language you’re writing in:

- understanding a problem and making a plan to solve it
- stepwise refinement of the code
- looping with a counter
- off-by-one errors
- variable declarations and types
- quoted string literals
- using the modulo operator to get remainder of integer division
- correct ordering of if/else if tests
- standard (console) output of strings and integers

## 0 comments:

Post a Comment