FizzBuzz

  • Comments posted to this topic are about the item FizzBuzz

  • These sorts of questions could be good. They could also lead down the slippery slope. If speed over quality is the most important - you will end up with a lot of bugs and rework.

    I have seen it several times. The devs know the code - but they are more concerned with speed. Under these circumstances, quality and standards are being pushed out the back door. This kind of test could be the type of thing to encourage such practices.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • For the kind of developers I like to hire, speed, performance, and quality all just happen. It doesn't take any longer to write quality high speed code than it does to write garbage that just happens to get the job done.

    I wouldn't set time limits on such tests... I'd just tell them to pretend they're writing code that will go into production and to give it their absolute best shot.

    As for the FizzBuzz test... it's a simple and well published solution so if I ever used it as part of a interview, I'd have to pork chop anyone who uses a loop or recursion to solve the problem. I'm not looking for someone who can't even count in a setbased fashion.

    Usually though... it doesn't take a test to be able to tell.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Heh... I just Googled "FIZZBUZZ SOLUTIONS SQL"... I'm amazed and surprised that some of those folks had the nerve to publish some of the solutions they have. Maybe it's a good test after all.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden (2/21/2010)


    Heh... I just Googled "FIZZBUZZ SOLUTIONS SQL"... I'm amazed and surprised that some of those folks had the nerve to publish some of the solutions they have. Maybe it's a good test after all.

    Did the same. Thought about doing a blog post about it. But then again, there are some decent ones out there that are a part of good blog posts already.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • Another option would be check out this puzzle roundup[/url] by Grant Fritchey.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • Jeff Moden (2/21/2010)


    I wouldn't set time limits on such tests... I'd just tell them to pretend they're writing code that will go into production and to give it their absolute best shot.

    I agree with Jeff about not setting a time limit 'cause I remember when I last went for a job interview I was so nervous that I shaked all over and had they asked me this question then with a time limit I would have failed bitterly. Now just for the heck of it and for those young 'uns going for a job interview, I quickly did this test and to put it together took me 2 minutes ok, maybe 4, but here is my query and please, give me your take on this.

    if isnull(object_id('tempdb..##rowtest'),0) <> 0 drop table ##rowtest

    select top 100 identity(int,1,1) idkey into ##rowtest from syscolumns

    select idkey, case when idkey % 3 = 0 then 'Fizz' else '' end [fizz],

    case when idkey % 5 = 0 then 'Buzz' else '' end [buzz],

    case when idkey%3 = 0 and idkey%5 = 0 then 'FizzBuzz' else '' end [fizzbuzz]

    from ##rowtest

    I give you the top 15 results.

    idkeyfizzbuzzfizzbuzz

    1

    2

    3Fizz

    4

    5Buzz

    6Fizz

    7

    8

    9Fizz

    10Buzz

    11

    12Fizz

    13

    14

    15FizzBuzzFizzBuzz

    :-PManie Verster
    Developer
    Johannesburg
    South Africa

    I can do all things through Christ who strengthens me. - Holy Bible
    I am a man of fixed and unbending principles, the first of which is to be flexible at all times. - Everett Mckinley Dirkson (Well, I am trying. - Manie Verster)

  • I checked out Grant Fritchey's[/url] article and was, like Jeff, not very pleased to see some while loops but one query attracted my attention and I tested it and I like it.

    ;WITH Nbrs(n) AS

    (

    SELECT 1

    UNION ALL SELECT 1 + n FROM Nbrs WHERE n < 100

    )

    SELECT CASE WHEN n%5=0 AND n%3=0 THEN 'BizzBuzz'

    WHEN n%3 = 0 THEN 'Bizz'

    WHEN n%5 = 0 THEN 'Buzz'

    ELSE CAST(n AS VARCHAR(8))

    END

    FROM Nbrs

    OPTION (MAXRECURSION 100);

    :-PManie Verster
    Developer
    Johannesburg
    South Africa

    I can do all things through Christ who strengthens me. - Holy Bible
    I am a man of fixed and unbending principles, the first of which is to be flexible at all times. - Everett Mckinley Dirkson (Well, I am trying. - Manie Verster)

  • It took me 14 minutes and I developed a solution which turned out to closely approximated one or more of what was on Fritchey's page.

    What is wrong with using a WHILE and what would be a preferable approach? Also, did you learn of this in a book or where?

    Thank you.

  • Manie Verster,

    I notice that you used a temporary table rather than a table variable. Why did you make that choice?

    Thank you.

  • I'm not a DBA and haven't used TSQL or coded with recordsets since around 2004, however I thought I'd have a go at this one. In VB took less than 2 minutes, in TSQL a little more as I had to see how to use MOD in tsql.

    The actual difficulty in this test (which I have to say I failed twice) isn't programming it, but ensuring the answer is exactly what was specified. From my student days I remember playing fizzbuzz as a drinking game and used the UK version, that includes the complication of fizz for numbers containing 3 (eg. 13) and buzz for numbers containing 5 (eg. 54) so I failed. I only noticed when I saw that the offered versions didn't and I double checked the requirements... I think however that all the solutions posted so far fail in that the request states:

    "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”. I would therefore expext

    3 Fizz

    to fail. Similarly I'd also interpret that a multiple of both fizz and buzz should only show fizzbuzz and not

    15 fizz buzz fizzbuzz

    Here's my code:

    (on viewing other entries I think a temporary table rather than a real one is probably better solution and the insert using the identity of another table instead of a while loop interesting)

    drop table dbo.fizzbuzz

    CREATE TABLE [dbo].[fizzbuzz](

    [num] [int] null,

    [value] [varchar](15) NULL

    ) ON [PRIMARY]

    GO

    --truncate table fizzbuzz

    SET ANSI_PADDING OFF

    declare @n as int

    select @n=1

    -- fill table with numbers

    while @n<=100

    begin

    insert into fizzbuzz (num) values (@n)

    select @n=@n+1

    end

    -- find fizzbuzzes

    update fizzbuzz set value='fizzbuzz'

    where num % 3=0 and num % 5 = 0

    -- find fizzes

    update fizzbuzz set value='fizz'

    where num % 3=0 --or CHARINDEX('3',cast(num as varchar(2)))>0

    and value is null

    -- find buzzes

    update fizzbuzz set value='buzz'

    where num % 5=0 --or CHARINDEX('5',cast(num as varchar(2)))>0

    and value is null

    -- fill in numbers

    update fizzbuzz set value=num

    where value is null

    select value from fizzbuzz

  • I first heard of this test when I was asked to do this in C# for a contract development role a few years ago. I was given a laptop with Visual Studio and told there was no Internet connection. No problem so far although I was a little surprised it was mentioned but was informed that the number of people who cannot do ANYTHING without Googling it first (or Binging etc) including creating a new project/solution. I offered them the opportunity to keep questioning me whilst I coded my solution but they declined my offer. Rather nice of them (turns out that they are a nice bunch of people).

    We then discussed my solution and I was offered the chance to critique my own code. They were wholly realistic in that they were not expecting production code. That is not to say they were expecting poor code. They were expecting me to know what I had omitted e.g. logging, debugging code, unit test, documentation (including inline - although there was some). They were expecting well formatted code with good naming, formatting etc. They were expecting the correct answer but understood it was a pressurised environment.

    It turns out that it is amazing how many people failed this test i.e. most.

    This test is a variant the old rope test. Plenty enough rope to hang yourself with!!!

    Gaz

    -- Stop your grinnin' and drop your linen...they're everywhere!!!

  • CirquedeSQLeil (2/21/2010)


    Another option would be check out this puzzle roundup[/url] by Grant Fritchey.

    Nuts. I was just going to point out that article I wrote several years ago. The comments in that one had some great (and bad) solutions.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • Hey Steve,

    I'm interested in your statement that team fit is important. I happen to agree with you.

    You might want to consult your HR department on this - if you have one. I think it may be illegal (in the UK) to discriminate on NON-OBJECTIVE grounds. Ie. "X scored lower than Y on the test" is an OBJECTIVE reason to not hire X. I suspect that "I could never work with him/her" is illegal (in the UK).

    If you don't hire X, it is possible you might have to defend your reasons in front of an employment tribunal.

  • This is really interesting and humbles me in my writing of T-SQL. I could do this in a snap using Visual Basic or C#; I am sure I could do it in SQL, but it probably would take me greater than ten minutes, I am sorry to say. In one interview I had for a programming job, they did want me (on a white board) to construct a Database Diagram for a simple hotel reaservation system. But yeah, this would be an interesting interview question that would make me sweat. Luckily my latest job bid is for a Project Management position.

Viewing 15 posts - 1 through 15 (of 363 total)

You must be logged in to reply to this topic. Login to reply