If the number’s a multiple of 3, print “foo” instead of the number.

If the number’s a multiple of 5, print “bar” instead of the number.

If the number’s a multiple of 3 and 5, print “foobar” instead of the number.

Can use pseudocode or the language of your choice.

(Note for non-programmers: this is a very easy task.)

(Note 2: Pseudocode is not real code with formal syntax; it’s just jotting down how code’s going to work.)

**Interview 1**

So this guy interviews today, seems nice and possibly adequate. According to him, his strength is VB.NET, and although 95% of our stuff is C#, I don’t sweat someone learning a new language syntax.

Towards the end of the interview, my colleague gave him the programming question above. After stammering for a few seconds, the guy explained, “Well, you know, pseudocode was one of the first classes I took, and I can’t really remember back that far.” While I tried to prevent myself from bursting, my colleague suggested he write out the code in Visual Basic, since that was supposed to be his strength. The shirking candidate claimed he couldn’t write the function without looking in a reference book.

At that moment, I felt like saying “Interview over, time for our next contestant.” However, my co-worker went on to explain the solution, which would use a loop to print out all the numbers. “A loop. You know, a for loop?” he asked. The candidate muttered, “Oh, a for loop…”, obviously with no knowledge of what it was (who knows, he may have been thinking it was a “4 loop”.)

**Interview 2**

This guy seems pretty sharp, and I thought he’d easily write the function we asked for. He knew what he wanted to do, but couldn’t remember the math operator to do it. “Mod? Square root?”, he muttered.

After a few minutes he came up with this bloat, where he rolled out his own test for mod equalling 0. He almost got the function right. As a point of comparison, the function can be done in C# in 5 lines, 7 if you count the braces.

We reminded him of what the mod operator does, and had a chuckle over it. I told him that if we hired him, I’d frame his solution above his desk. Er, cubicle.

I am a definite amateur at programming, all I learned was from parts of a book on 2-d game programming, and since i haven’t done any coding in a while this will be pseudo code. (i have done some coding but not much, im most proud of my kitchen timer program, drawing programs[very very basic ones] and a very messed up text based rpg with some graphics and sound. i liked the last best the second second best.) this is my probably not so good attempt at answering the question. took a few minutes so probably did worse than all of you’r interviewers, but still seemed like a fun thing to try:

number variable = 0

begin loop here

add 1 to “number”

If number>100 then waitkey and end program

If number divided by 3 = a whole number ;(not sure how to check for that, maybe could figure out a lengthy way to do it if i cared more.)

then print “foo” and go to label#1 if not then go to label#2

if number divided by 5 = a whole number then print “bar” and go to lable#1 if not then go to label#2

if number divided by 3 = a whole number andif number divided by 5 = a whole number then print “foobar” and go to label#1 if not then go to label#2

Label#2

print number

label#1

end loop here ;i think “wend” is the term i recall.

Wend! Sounds like old-school BASIC! You can check for divisibility using Mod, which returns the remainder after division (well, for positive numbers)

10 Mod 4 = 2

10 Mod 5 = 0

You did a lot better than the first guy, who essentially handed in a blank sheet of paper. And it’s almost right.

Interestingly, both you and Interview 2 made the same mistake of checking for the divide by 15 last. It actually should be checked first (I’ll let you figure out why that needs to be the case).

Wait. Why divide by 15 at all? If something is divisible by both 3 and 5, isn’t it divisible by 15 then? If so, then you don’t need to do a seperate calculation.

for( int i = 1; i < 101; i++ ){ string Result = ""; if( i % 3 == 0 ) Result = "foo"; if( i % 5 == 0 ) Result += "bar"; Result = "" ? console.writeline(i) : console.writeline (Result); }

ahh good point, if it is divisible by 3 or five then it will immediately print foo or bar when it could be divisible by both and should print foobar. it was a fun try anyway.

so i guess to use mod to check it it would be like: if number mod 3 = 0 andif number mod 5 = 0 then etc.?

The programming language i learned from was specifically made for making games, called blitz basic. mostly 2d games it seems so you are right it probably is pretty old. Doing this makes me want to learn more programming again, i once tried to find a free compiler that would work well with some free online programming lessons i found but i couldnt figure somethings out about the compiler for some reason. maybe someday ill just buy a book that comes with a compiler.

p.s. do all numbers divisible by both 3 and 5 also divisible by 15? guess i have forgotten some algebra.

If the language has a way to print characters without printing a new line, Allen was right about not having to check for 15 at all.

The solution would be something like this:

for i = 1 to 100

printnumber = true

if (i mod 3 = 0) then

print “foo”

printnumber = false

end if

if (i mod 5 = 0) then

print “bar”

printnumber = false

end if

if printnumber = true then

print i

end if

print [newline character]

end for loop

(Or, it can be done in 5 lines as Allen did, but Basic doesn’t have all the stuff that C# does.)

On the other hand, if the print command always attaches a newline character at the end, you have to check for 15 separately (I don’t know if this was the case for Blitz Basic.)

PS: Yes. All numbers divisible by two numbers is divisible by the product of those numbers.

Donnie – I lack any knowledge of programming so I never have anything substantive to note on these interview posts, but the whole thing is utterly fascinating so I hope you’ll keep going.

thx, can’t believe i forgot about end if. would have used it incorrectly anyway probably.