5 ms·
The interview question isn't supposed to test your knowledge of Pascal's Triangle - it's designed to test your ability to create it programatically. I'm sure c
by 2arrs2ells 15y ago
The interview question isn't supposed to test your knowledge of Pascal's Triangle - it's designed to test your ability to create it programatically.
I'm sure cletus or any other interviewer would explain what Pascal's Triangle is to you, and provide a couple rows of example output if you asked.
EDIT: Find/replace "Pascal's Triangle" with "FizzBuzz" and reread the comment. You'll get the same insight without getting hung up on seeing a term you didn't know.
- jebblue 15y agoWhat is a FizzBuz and what does it have to do with day to day Software Engineering?
- switz 15y agoFizzBuzz: Write a program that prints the numbers 1 to 100. However, for multiples of 3 print "Fizz" and for multiples of 5 print "Buzz". For multiples of both 3 and 5, print "FizzBuzz".
- harichinnan 15y agoCouldn't resist using my new Haskell skills. show' :: Int -> String show' x |((rem x 3 == 0) && (rem x 5 == 0)) = "FizzBuzz" |rem x 3 == 0 = "Fizz" |rem x 5 == 0 = "Buzz" |otherwise = show x main :: IO () main = putStrLn $ foldl (\acc x -> acc ++"\n" ++ show' x) "" [1..100]
- tikhonj 15y agoA couple of suggestions: functions like rem look better infix: x `rem` 3 this means the same thing but is more readable (I think). Instead of folding, just map putStrLn over the list: mapM_ (putStrLn . show') [1..100] mapM_ is just a version of map for monads that ignores its result (it returns m () instead of m a). You can also use forM_ which is mapM_ with its arguments reversed. Finally, I think this site lets you format code by putting four spaces in front of each line. I hope :) Edit: One other thing. You don't need all those parentheses in the first line. rem x 3 == 0 && rem x 5 == 0 = "FizzBuzz" x `rem` 3 == 0 && x `rem` 5 == 0 = "FizzBuzz" Both versions should work and I think they are easier to read. I could see (rem x 3 == 0) also being clear, but the outermost parentheses do not help at all.
- it 15y agoHere's a shorter version: fizzbuzz x | x `mod` 3 == 0 && x `mod` 5 == 0 = "FizzBuzz" | x `mod` 3 == 0 = "Fizz" | x `mod` 5 == 0 = "Buzz" | otherwise = show x main = putStr $ unlines $ map fizzbuzz [1..100]
- rayval 15y agoFor us old-school types, here's the 60-second version in C: int main(int argc, char ** argv) { int i; for (i=0; i< 100; i++) { if( (i%3==0)&&(i%5==0)) printf("FizzBuzz"); else if(i%3==0) printf ("Fizz"); else if(i%5==0) printf ("Buzz"); else printf("%d",i); printf("\n"); } }
- acheron 15y agoI've gotten that one a few times, and I always write something that only contains the string 'Fizz' once and 'Buzz' once (i.e., doesn't say 'FizzBuzz' separately, just uses the same logic to print 'Fizz' and 'Buzz' both), because I figured that was partly the point; otherwise it would be "print 'Zarples' for both multiples". I've been told that it's the first time the interviewer saw it done that way, and now I see several replies here that also would take no modification to use 'Zarples' instead of 'FizzBuzz'. No point to this, I guess, other than that even something as simplistic as this is open to interpretation.
- fishtoaster 15y agoWhat switz said. It's useful as a very early screening because it eliminates a lot of people who are wasting your time and is so simple as to have almost to false negatives. It has no complexity, no algorithm or concepts you might be unfamiliar with- just the for loop, the conditional, and the modulo operator. No one who claims to be a professional developer should fail it. For reference: http://www.codinghorror.com/blog/2007/02/why-cant-programmers-program.html http://www.codinghorror.com/blog/2007/02/why-cant-programmer...
- earl 15y agoA trivial piece of code that a candidate should be able to write as quickly as they can type on a computer or write on the board designed to test that (1) they can write basic code and (2) they've written enough code that trivial problems are trivial. Jeff Atwood coined the phrase [1] and gave the classic example: 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". [1] http://www.codinghorror.com/blog/2007/02/why-cant-programmers-program.html http://www.codinghorror.com/blog/2007/02/why-cant-programmer...
- digitailor 15y agoCan someone tell me a lispier way of doing this than (dotimes (n 100) (cond ((= 0 n)) ((and (= 0 (mod n 3))(= 0 (mod n 5))) (format t "~a: Fizz Buzz!~%" n)) ((= 0 (mod n 3)) (format t "~a: Fizz!~%" n)) ((= 0 (mod n 5)) (format t "~a: Buzz!~%" n)) (t (format t "~a~%" n))))
- ericbb 15y agoSurely, the lispiest way is to write an interpreter! Write a function fizzbuzz-eval so that the following is a solution (left as an exercise). ;) (fizzbuzz-eval 100 '(((3 5) (print "FizzBuzz")) ((3) (print "Fizz")) ((5) (print "Buzz")) (() print-number)))
- deleted 15y ago[deleted]
- digitailor 15y agoHere's a macro. There's some issues :) The biggest one is you have to make sure to put the multiple divisors list first. The call is (fizzbuzz (100 (((3 5) "fizzbuzz")(3 "fizz")(5 "buzz")))) (defun make-cond (item) (let ((divisors (first item)) (exclamation (second item))) (if (listp divisors) (let ((div1 (first divisors)) (div2 (second divisors))) `((and (= 0 (mod n ,div1)) (= 0 (mod n ,div2))) (format t "~a: ~a~%" n ,exclamation))) `((= 0 (mod n ,divisors)) ;else (format t "~a: ~a~%" n ,exclamation))))) (defmacro fizzbuzz (params-list) (destructuring-bind (num-to req-list) params-list (let ((cond-list '(((= 0 n))))) (loop for item in req-list do (push (make-cond item) cond-list)) `(dotimes (n ,num-to) (cond ,@(reverse (push '(t (format t "~a~%" n)) cond-list)))))))
- Jacquass12321 15y agoVery much this, my company has been taking a deeper look at its hiring processes lately, and I know there are explicitly comments on our small coding problems that say things like "Candidates might get hung up on the gotcha part of it: the actual snip calculation. That's not something we care about, but that the candidates will likely think we care about. We should consider just giving them the formula (or letting them use a pre-defined "snip" method)."