pouët.net

Go to bottom

questions for programmers interview

category: code [glöplog]
graga, it's easy to steal the show if you ensure the group has 3 or 5 people. make the guy left of you start.
added on the 2012-07-01 10:56:57 by skrebbel skrebbel
skrebbel: not bad but can you make it multithreaded?
added on the 2012-07-01 11:40:26 by Gargaj Gargaj
something like
Code: string[] output = new string[count]; Parallel.For(startIndex, count, (i) => { output[i - startIndex] = generator.GetNumberForIndex(i); }, ); output.ForEach(Console.WriteLine);
added on the 2012-07-01 11:54:17 by Gargaj Gargaj
now do it in brainfuck!
added on the 2012-07-01 13:27:40 by maali maali
"At least 2 year of experience in LOLcode required."
added on the 2012-07-01 13:38:27 by ham ham
Skrebbel: Good effort, the initialization code in Main() could use a registry singleton though. Also what Gargaj said.
added on the 2012-07-01 14:21:50 by kb_ kb_
I'm actually wondering if it could be done in LINQ.
added on the 2012-07-01 14:33:36 by Gargaj Gargaj
There's some serious effort, that could have gone intro prods, being wasted in this thread! :D
added on the 2012-07-01 14:37:14 by Punqtured Punqtured
All you need to ask is: Amiga or PC?
added on the 2012-07-01 15:40:38 by maytz maytz
Maxim: Any discussion about interviewing will eventually degrade into a discussion of people implementing FizzBuzz.
added on the 2012-07-01 18:24:25 by Sesse Sesse
I think skrebbel's solution lack both inheritance and factories. I mean, who doesn't find this appealing?

Code: class Node { public: Node(int number) : m_number(number) { } virtual std::string getString() const { std::stringstream out; out << m_number; return out.str(); } private: int m_number; }; class FizzNode : public Node { public: FizzNode(int number) : Node(number) { } std::string getString() const { return std::string("Fizz"); } }; class BuzzNode : public Node { public: BuzzNode(int number) : Node(number) { } std::string getString() const { return std::string("Buzz"); } }; class FizzBuzzNode : public Node { public: FizzBuzzNode(int number) : Node(number) { } std::string getString() const { return std::string("FizzBuzz"); } }; class FizzBuzzFactory { public: static Node *GetNode(int i) { bool is_fizz = i % 3 == 0, is_buzz = i % 5 == 0; if (is_fizz && is_buzz) return new FizzBuzzNode(i); if (is_fizz) return new FizzNode(i); if (is_buzz) return new BuzzNode(i); return new Node(i); } };
added on the 2012-07-01 21:13:49 by kusma kusma
he does have inheritance and he does make cretive use of it, while you're just delegating the actual printf. ;)
added on the 2012-07-01 21:23:15 by Gargaj Gargaj
how about
Code:<? $opts = array(3=>"Fizz",5=>"Buzz"); for ($x=1; $x<=100; $x++) { $a = array(); foreach($opts as $div=>$word) if ($x % $div == 0) $a[] = $word; echo ($a ? implode("",$a) : $x)."\n"; } ?>
? :)
added on the 2012-07-01 21:37:52 by Gargaj Gargaj
Well, as far as php goes, it's pretty good. ;)
added on the 2012-07-01 21:48:27 by tomaes tomaes
I'll join the club with LINQ:
Code: static void Main(string[] args) { foreach (var s in Enumerable.Range(1, 100) .Select(i => i % 15 == 0 ? "FizzBuzz" : i % 5 == 0 ? "Buzz" : i % 3 == 0 ? "Fizz" : i.ToString())) Console.WriteLine(s); Console.ReadLine(); }
added on the 2012-07-01 22:20:42 by xTr1m xTr1m
Fine, I'm bored, so here's a c89 version (remix, not following the task to the letter here. Not the prettiest version I can think of either. :P):

Code: int main( int argc, char ** argv ) { int i = 0; for(; i++ < 0x3E8;) printf("(<%d>) f? %s, b? %s, fb? %s\n", i, (i%3)==0?"ok":"no", (i%5)==0?"sure":"no", (i%15==0)?"yep":"no" ); return 0; }
added on the 2012-07-01 23:18:54 by tomaes tomaes
Gargaj: It's not absent, but it was a golden opportunity to add some more!
added on the 2012-07-01 23:19:10 by kusma kusma
that's it, i'm opening a sourceforge repo! :D
added on the 2012-07-01 23:24:01 by Gargaj Gargaj
Couldn't resist: real decent LINQ, without conditionals

Code: var n = from i in Enumerable.Range(1, 100) join e in ( from j in Enumerable.Range(1, 100) from el in new[] { new { k = j, v = j.ToString() }, new { k = j * 3, v = "Fizz"}, new { k = j * 5, v = "Buzz"}, new { k = j * 15, v = "FizzBuzz"}, } select el ) on i equals e.k into m select m.First().v; n.ToList().ForEach(Console.WriteLine);


admittedly, this is getting absolutely ridiculous, though.
added on the 2012-07-01 23:30:30 by skrebbel skrebbel
skrebbel: "is getting"? :)
added on the 2012-07-01 23:37:45 by kusma kusma
Slightly more dandy c version:

Code: int main(int argc, char** argv) { int i = 0; for(; i++ < 0x3E8;) printf("\n(<%d>) %s%s%s", i, !(i%3)?"fizz ":"", !(i%5)?"buzz ":"", !(i%15)?"fizzbuzz":"" ); return 0; }
added on the 2012-07-01 23:44:27 by tomaes tomaes
Code:@ECHO OFF GOTO start :loop_body SET /a N=1+%1*10+%2 SET /a NMOD3="%N% - (%N% / 3 * 3)" SET /a NMOD5="%N% - (%N% / 5 * 5)" SET /a COND=%NMOD3% * %NMOD5% if '%NMOD3%'=='0' ECHO|SET /p=Fizz if '%NMOD5%'=='0' ECHO|SET /p=Buzz if '%COND%'=='0' GOTO skip_print ECHO|SET /p=%N% :skip_print ECHO. EXIT /b :subloop FOR %%J IN (0 1 2 3 4 5 6 7 8 9) DO CALL :loop_body %1 %%J EXIT /b :start FOR %%I IN (0 1 2 3 4 5 6 7 8 9) DO CALL :subloop %%I
added on the 2012-07-01 23:54:04 by ponce ponce
This is fun!
Code: #include <stdio.h> void fizz(int i); void buzz(int i); void number(int i) { printf("%d\n", i++); if (i % 3 == 0) fizz(i); else if (i % 5 == 0) buzz(i); else number(i); } void fizz(int i) { fputs("Fizz", stdout); if (i % 5 == 0) return buzz(i); ++i; putchar('\n'); if (i % 5 == 0) buzz(i); else number(i); } void buzz(int i) { puts("Buzz"); if (i == 100) return; ++i; if (i % 3 == 0) fizz(i); else number(i); } int main(int argc, char *argv[]) { number(1); }
added on the 2012-07-02 00:39:54 by kusma kusma
skrebbel: glorious! :)
added on the 2012-07-02 01:05:24 by xTr1m xTr1m

login

Go to top