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.
skrebbel: not bad but can you make it multithreaded?
something like
Code:
string[] output = new string[count];
Parallel.For(startIndex, count, (i) =>
{
output[i - startIndex] = generator.GetNumberForIndex(i);
},
);
output.ForEach(Console.WriteLine);
now do it in brainfuck!
"At least 2 year of experience in LOLcode required."
Skrebbel: Good effort, the initialization code in Main() could use a registry singleton though. Also what Gargaj said.
I'm actually wondering if it could be done in LINQ.
There's some serious effort, that could have gone intro prods, being wasted in this thread! :D
All you need to ask is: Amiga or PC?
Maxim: Any discussion about interviewing will eventually degrade into a discussion of people implementing FizzBuzz.
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);
}
};
he does have inheritance and he does make cretive use of it, while you're just delegating the actual printf. ;)
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";
}
?>
Well, as far as php goes, it's pretty good. ;)
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();
}
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;
}
Gargaj: It's not absent, but it was a golden opportunity to add some more!
that's it, i'm opening a sourceforge repo! :D
Couldn't resist: real decent LINQ, without conditionals
admittedly, this is getting absolutely ridiculous, though.
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.
skrebbel: "is getting"? :)
gargaj: Dude, get with the times!
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;
}
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
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);
}
skrebbel: glorious! :)