views:

1739

answers:

14

Anybody have any good FizzBuzz type questions that are not the FizzBuzz problem?

I am interviewing someone and FB is relatively well known and not that hard to memorize, so my first stop in a search for ideas is my new addiction SO.

+4  A: 

Fibonacci, reverse a string, count number of bits set in a byte are other common ones. Project Euler also has a large collection of increasing difficulty.

Tom Ritter
+2  A: 

Ask them to write an app to return the factors of a given number. It's easy to do and hard to do well in a short period of time. You can see their style and the way they think through problems in a small amount of time.

Cody Brocious
+1  A: 

How about: I want to use a single integer to store multiple values. Describe how that would work.

If they don't have a clue about bit masks and operations, they probably can't solve other problems.

torial
I would say it's more instructive if after having bit masks explained, or pointed out, if the person doesn't smack their forehead, and shake their head in self-mockery. Bit-masks aren't a common idiom, unless one does C, embedded devices, or networking. Lots of talented folk haven't.
Gregg Lind
Hmm, then you have to decide if accepting storing 1,2 and 3 in decimal 123 counts as a correct answer, even though the math would be ugly complicated compared to just declaring 3 variables. Or storing 1,2,3 by writing x=1; x=2; x=3; I mean, do we need to store these values contemporaneously?
MatthewMartin
A: 

Find a list of primes is a fairly common question but it still requires some thought and there are varying degrees of answers people might give.

You would also be surprised how many people struggle to implement a Map/Dictionary type data-structure.

Chris
+6  A: 

I've found checking a string if it is a palindrome is a pretty simple one that can be a decent weeder.

Mike Stone
I'd say that depends on the language. In C it could be interesting, in Perl it's done with `scalar(reverse 'foo') == 'foo'`.
jkramer
true, but at the same time, being able to see the simpler solution is valuable... then you say, "ok, now pretend you don't have the reverse function."
Mike Stone
In C++, I'd give bonus points for any "functional" solutions that don't involve a hand-written loop. e.g., "return equal(str.begin(), str.end(), str.rbegin());" or (for speed freaks) "return equal(str.begin(), str.begin() + str.size() / 2, str.rbegin());"
Chris Jester-Young
Of course, upon seeing such an answer, I'd also ask the candidate to explain the working of the code. They can't get a leg up just by copying my answer above! :-P
Chris Jester-Young
A: 

Check out 6.14 from the C++ FAQ Lite:

http://www.parashift.com/c++-faq-lite/big-picture.html

John at CashCommons
+11  A: 

I've seen a small list of relatively simple programming problems used to weed out candidates, just like FizzBuzz. Here are some of the problems I've seen, in order of increasing difficulty:

  1. Reverse a string
  2. Reverse a sentence ("bob likes dogs" -> "dogs likes bob")
  3. Find the minimum value in a list
  4. Find the maximum value in a list
  5. Calculate a remainder (given a numerator and denominator)
  6. Return distinct values from a list including duplicates (i.e. "1 3 5 3 7 3 1 1 5" -> "1 3 5 7")
  7. Return distinct values and their counts (i.e. the list above becomes "1(3) 3(3) 5(2) 7(1)")
  8. Given a string of expressions (only variables, +, and -) and a set of variable/value pairs (i.e. a=1, b=7, c=3, d=14) return the result of the expression ("a + b+c -d" would be -3).

These were for Java, and you could use the standard libraries so some of them can be extremely easy (like 6). But they work like FizzBuzz. If you have a clue about programming you should be able to do most pretty quickly. Even if you don't know the language well you should at least be able to give the idea behind how to do something.

Using this test one of my previous bosses saw everything from people who aced it all pretty quick, to people who could do most pretty quick, to one guy who couldn't answer a single one after a half hour.

I should also note: he let people use his computer while they were given these tasks. They were specifically instructed that they could use Google and the like.

MBCook
For item 8, would a solution based on JSR-223 (javax.script) be accepted? :-P (Admittedly the use of that is completely overkill, but some people would rather do that than use, say, java.util.Scanner.)
Chris Jester-Young
That's not within my idea of the spirit of the question, but if you know enough to propose that, then you certainly know enough to pass the FizzBuzz questions, so I wouldn't hold it against you. It may even be a plus in your favor. I'd still probably ask how you'd do it without javax.script though.
MBCook
+1  A: 

Return the index of the first occurrence of string X within string Y

Implementing strstr() requires a basic understanding of the language while providing the opportunity for clever optimization.

nilihm
+2  A: 

If it is a C/C++ interview make sure the person knows about pointers.

General - simple algorithm ([single/double]linked list). Ask about complexity of adding in each case (at the begining, at the end, optimizations ...) ?

(General) How do you find min and max from an array (N size) with just 3*N/2 comparisons?

C/C++: How would you optimize multiple "strcat"s to a buffer ?

Iulian Şerbănoiu
It seems to me that for the problem "How do you find min and max from an array (N size) with just 3*N/2 comparisons?" it is good to clarify that number 3*N/2 is a number of comparison of elements of array, but you can compare int freely for example. e.g. (i < array size) in loops.
sergdev
+5  A: 

Any of the early ones from Project Euler would probably be good.

shelfoo
+10  A: 

Perhaps this does not answer your question directly, but I am not certain you need to come up with another problem. Besides being "easy to memorize", the FizzBuzz question is just plain "easy", and that is the point. If the person you are interviewing is in the class of people to which FizzBuzz is "well-known", then they are in the class of people that a FizzBuzz-type question would not filter out. That does not mean that you hire them on the spot, but it does mean that they should be able to breeze through it and get on to the meat of the interview.

To put it another way, anybody who takes the time to read Coding Horror is worth interviewing further. Just have them write out the solution really quickly, discuss it briefly (e.g., How do you test this?), and then move on to the next question. And as the article says, "it is genuinely astonishing how many candidates are incapable of the simplest programming tasks."

Brandon DuRette
A: 

I have asked my candidates to create a program to calculate factorial of a given number in any pseudo language of their choice. It is a fairly easy problem to solve and it lends itself well to the natural followup quistions (that could often be asked) about recursion.

UlfR
+1  A: 

I wanted a FizzBuzz question that doesn't involve the modulo operator. Especially since I'm typically interviewing web developers for whom the modulo operator just doesn't come up that often. And if it's not something you run into regularly, it's one of those things you look up the few times you need it.

(Granted, it's a concept that, ideally, you should have encountered in a math course somewhere along the way, but that's a different topic.)

So, what I came up with is what I call, unimaginatively, Threes in Reverse. The instruction is:

Write a program that prints out, in reverse order, every multiple of 3 between 1 and 200.

Doing it in normal order it easy: multiply the loop index by 3 until you reach a number that exceeds 200, then quit. You don't have to worry about how many iterations to terminate after, you just keep going until you reach the first value that's too high.

But going backwards, you have to know where to start. Some might realize intuitively that 198 (3 * 66) is the highest multiple of 3, and as such, hard-code 66 into the loop. Others might use a mathematical operation (integer division or a floor() on a floating point division of 200 and 3) to figure out that number, and in doing so, provide something more generically applicable.

Essentially, it's the same sort of problem as FizzBuzz (looping through values and printing them out, with a twist). This one is a problem to solve that doesn't use anything quite as (relatively) esoteric as the modulo operation.

Legion
I am curious Legion: how do your web developers do things such as green-barring/alternate rows without modulo?
Andrew Burns
Well, if you're just trying to apply styles to alternating rows, by using CSS3's nth-child selector. jQuery has alternating selectors too for doing such things through JS. But speaking to your larger point, like I said above, it's something that gets looked up, used, and then quickly forgotten because it took all of 15 seconds to find. I'm not saying I like it or approve, but especially at entry level, it happens. :)
Legion
A: 

For something really super-simple that can be done in 10 seconds, but would remove those people who literally can't program anything, try this one:

Ask: show me (on paper, but better on a whiteboard) how you would swap the values of two variables.

This wasn't my idea, but was posted in a comment by someone named Jacob on a blog post all about the original FizzBuzz question.

Jacob goes on to say:

If they don’t start with creating a third variable, you can pretty much write that person off. I’ve found that I can cut a third to half my (admittedly at that point unscreened) applicants with that question alone.

There is a further interesting discussion after that comment on the original blog post about ways to perform this variable swapping without requiring a third variable (adding/subtracting, xor etc.), and of course, if you're using a language that supports this in a single statement/operation, it may not be such a good test.

Although not my idea, I wanted to post this here as it's such an elegantly simple, easy question that can (and should) be answered within about 10 seconds by someone who has written even the simplest of programs. It also does not require the use of somewhat apparently obscure operators like the modulo operator, which lots of people, who are otherwise fairly decent programmers, are simply not familiar with (which I know from my own experience).

CraigTP