How would you solve this programming interview question?

Discussion in 'OT Technology' started by White Stormy, Feb 25, 2010.

  1. White Stormy

    White Stormy Take that, subspace!

    Joined:
    Sep 17, 2002
    Messages:
    85,486
    Likes Received:
    69
    Location:
    Sparkopolis
  2. White Stormy

    White Stormy Take that, subspace!

    Joined:
    Sep 17, 2002
    Messages:
    85,486
    Likes Received:
    69
    Location:
    Sparkopolis
    This is what I would write..
    Code:
    <?php
    for ($i=1; $i<101; $i++)
    {
    	if ($i%5==0 && $i%3==0)
    		print 'FizzBuzz ';
    	else if ($i%5==0)
    		print 'Buzz ';
    	else if ($i%3==0)
    		print 'Fizz ';
    	else
    		print $i . ' ';
    	
    }
    ?>
    
    Is there something better than that?
     
  3. ge0

    ge0 New Member

    Joined:
    Oct 31, 2005
    Messages:
    8,398
    Likes Received:
    0
    Location:
    JERSEY
    using mod is pretty much it.
     
  4. CodeX

    CodeX Guest

    you only need 2 comparisons, you are using 4
     
  5. White Stormy

    White Stormy Take that, subspace!

    Joined:
    Sep 17, 2002
    Messages:
    85,486
    Likes Received:
    69
    Location:
    Sparkopolis
    how would you only use 2 and still get the numbers to print correctly?

    Code:
    if ($i%3==0) print 'Fizz';
    if ($i%5==0) print 'Buzz';
    
    doesn't print the numbers at all

    Code:
    if ($i%3==0) print 'Fizz';
    else print $i;
    if ($i%5==0) print 'Buzz';
    else print $i;
    
    prints 7 twice. only one else won't work, either.
     
  6. CodeX

    CodeX Guest

    Use the continue keyword
     
  7. mobbarley

    mobbarley Active Member

    Joined:
    Mar 4, 2005
    Messages:
    9,256
    Likes Received:
    2
    Location:
    Sydney
    google fizz buzz, it has to be the most common one out there :rolleyes:
     
  8. CodeX

    CodeX Guest

    Oh you're using php, in C and similar languages you can use continue to end the loop iteration and proceed to the next.
     
  9. White Stormy

    White Stormy Take that, subspace!

    Joined:
    Sep 17, 2002
    Messages:
    85,486
    Likes Received:
    69
    Location:
    Sparkopolis
    language doesn't matter. I hadn't thought of it like that. :cool:

    PHP has a continue statement, btw.
     
  10. White Stormy

    White Stormy Take that, subspace!

    Joined:
    Sep 17, 2002
    Messages:
    85,486
    Likes Received:
    69
    Location:
    Sparkopolis
    STFU!@!
     
  11. CodeX

    CodeX Guest

    Yeah, one thing I learned in my embedded systems class that stuck with me was to short circuit unnecessary comparisons whenever possible, especially in loops.

    Wasn't sure, I've only used PHP for a few weeks several years ago.

    This is what I was thinking, couldn't write it last night I was on my ipod:
    Code:
    for(i = 1; i < 101; i++)
    {
     if(i%5)
     {
      print "fizz"
      if(i%3) print "buzz"
      continue
     }
    
     if(i%3) print "buzz"
     else print i
    }
    
    There are 3 comparisons in code, but at most 2 along any logic path. What you wrote will typically make 3 comparisons.
     
    Last edited by a moderator: Feb 25, 2010
  12. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    I don't have the code with me, but we just did this at work as an example of using test driven development. Toss some TDD in there for bonus points depending on the shop you're applying for.
     
  13. Lurker111

    Lurker111 New Member

    Joined:
    Sep 23, 2003
    Messages:
    1,837
    Likes Received:
    0
    Location:
    Back in Seattle
    That won't work.

    5%5 = 0 so it will never print fizz for 5.
    3%3 = 0 so it will never print buzz for 3.
    anything not a multiple of 3 or 5 will pass your conditional tests and print fizzbuzz.
     
    Last edited: Feb 27, 2010
  14. Lurker111

    Lurker111 New Member

    Joined:
    Sep 23, 2003
    Messages:
    1,837
    Likes Received:
    0
    Location:
    Back in Seattle
    A more interesting question is to reverse a string in place without allocating any additional memory beyond what you have for the original null terminated array (in C). A friend of mine was thrown this one at lunch while going through a day long interview process. Take a bite of your sandwich and then have to swim through a sea of pointers...tough to do when you don't have at least paper and pencil handy to throw down some ideas to form the answer.
     
  15. White Stormy

    White Stormy Take that, subspace!

    Joined:
    Sep 17, 2002
    Messages:
    85,486
    Likes Received:
    69
    Location:
    Sparkopolis
    he was just writing pseudocode to show the comparison paths. he also had Fizz and Buzz assigned to the wrong numbers, and didn't use any semi-colons, but the logic still works

    Code:
    for (i = 1; i < 101; i++)
    {
    	if (i % 3 == 0)
    	{
    		print "Fizz";
    		if (i % 5 == 0) print "Buzz";
    		continue;
    	}
    
    	if (i % 5 == 0) print "Buzz";
    	else print i;
    }
    
     
  16. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    Ah, here's the code I had worked on:

    Code:
    /*
     * http://www.codinghorror.com/blog/archives/000781.html
     * 
     * 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". 
     */
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace fizzbuzz
    {
    	class Program
    	{
    		private const int MAXCOUNT = 100;
    		private const string FIZZ = "fizz";
    		private const string BUZZ = "buzz";
    		private static readonly Dictionary<int, string> RULES = new Dictionary<int, string>()
    			{
    				{3, FIZZ},
    				{5, BUZZ},
    			};
    
    		static void Main(string[] args)
    		{
    			outputFizzBuzz();
    		}
    
    		private static void outputFizzBuzz()
    		{
    			for(int a = 1; a < MAXCOUNT + 1; a++)
    			{
    				if (a > 1)
    				{
    					Console.Write(" ");
    				}
    
    				Console.Write(getFizzBuzz(a));
    			}
    		}
    
    		
    		private static string getFizzBuzz(int a)
    		{
    			var outputBuilder = new StringBuilder();
    
    			foreach(var key in RULES.Keys)
    			{
    				outputBuilder.Append(fizzBuzzValue(a, key));
    			}
    
    			return outputBuilder.Length > 0 ? outputBuilder.ToString() : a.ToString();
    		}
    
    
    		private static string fizzBuzzValue(int testvalue, int rulekey)
    		{
    			return (testvalue % rulekey == 0) ? RULES[rulekey] : "";	
    		}
    		
    	}
    }
    
    
     
  17. ge0

    ge0 New Member

    Joined:
    Oct 31, 2005
    Messages:
    8,398
    Likes Received:
    0
    Location:
    JERSEY
    I do love when people pick PHP for their "PROGRAMMING" questions.. haha
     
  18. White Stormy

    White Stormy Take that, subspace!

    Joined:
    Sep 17, 2002
    Messages:
    85,486
    Likes Received:
    69
    Location:
    Sparkopolis
    1) language doesn't matter
    2) you're retarded
     
  19. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    I see no problem picking PHP for this. You should be able to do this problem in a couple different languages including one you are not an expert at (or are learning).
     
  20. CodeX

    CodeX Guest

    pseudocode my friend, I was just showing the logic. You're right, I forgot the "== 0" in the tests, whatever Stormy knew what I meant.
     
  21. kingtoad

    kingtoad OT Supporter

    Joined:
    Sep 2, 2003
    Messages:
    55,914
    Likes Received:
    8
    Location:
    Los Angeles
    Choice of language has very little to do with a problem like this.
     
  22. ge0

    ge0 New Member

    Joined:
    Oct 31, 2005
    Messages:
    8,398
    Likes Received:
    0
    Location:
    JERSEY

    Not saying this problem. I am saying in general.
     
  23. kingtoad

    kingtoad OT Supporter

    Joined:
    Sep 2, 2003
    Messages:
    55,914
    Likes Received:
    8
    Location:
    Los Angeles
    I like fuzzy bunnies.
     
  24. White Stormy

    White Stormy Take that, subspace!

    Joined:
    Sep 17, 2002
    Messages:
    85,486
    Likes Received:
    69
    Location:
    Sparkopolis
    I hate fuzzy bunnies! :madfawk:
     
  25. CodeX

    CodeX Guest

    Use the null terminator as scratch space...

    //Original
    "Test/0"

    //copy first char to null terminator, clear it (for example only, you don't have to clear the character location that was just copied but it makes it easier to follow the logic)
    /0estT

    //copy second to last char to first char, clear it
    tes/0T

    //...etc.
    t/0seT

    //Done, move null terminator to end of string
    ts/0eT

    tseT/0

    This only works if the length of the string can be assumed, otherwise it must be recorded, so technically it would use 1 extra byte of memory. I can't think of a better way.

    (yes, thread resurrection, I never saw this problem posted and love problems like this)
     

Share This Page