Tuesday 27 April 2010

The way to solve fizz and buzz questions on interviews

Hi,
I,ve got and interview yesterday in a small company, I was applying for software engineers role as an senior developer`s assistant. You know how it is hard to have a hope and compete with some others maybe graduated programmers since you do not have the educational back ground. Well I would say don,t give up you have same chances as graduates.
OK, thing I want to share with is technical question I failed, I mean answer. So, popular interview question "fizz" and "buzz", or write a program that prints out, lets say numbers from 1 -> 100 and for numbers multiplied with 3 prints out "fizz", for numbers multiplied by 5 prints out 5, I guess there was a third rule - For numbers multiplied by 3 and 5 print out "fizz and buzz". So the trick here is to use modulus or mod operator... Actually it is not a trick it is correct answer.
Below are two versions 1st in JavaScript - as function, second in python... I hope to have comments or suggestions. Sorry with out rule No 3..
JavaScript code:
function mod_calc(){
for (i=1; i<=60; i++) { if (i % 3 == 0) { document.write("fizz" + " "); } else if (i % 5 ==0) { document.write("buzz" + " "); } else { document.write("Number" + i + " "); } } } and Python code: for i in range(1, 31): if i % 3 == 0: print 'fuzz' elif i % 5 == 0: print 'buzz' else: print 'Number', i These are my options and solutions, so I am open for any other ones.... Oh yes I use in examples only numbers from 1 -> 30, its just faster... You know...

No comments:

Post a Comment