Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

First off, there are equally important questions for a candidate that are non-technical (do they work well with others, can they imagine a better approach, etc.). But to evaluate the technical skills, what would be your one question?

I ask because I have one. I have no idea why this question works so well. I don't even have a good theory. All I have is an incredible correlation between answering this question correctly and being a really strong programmer.

The question was suggested to me by someone I respect a lot. I replied that it seemed too easy but he suggested I try it. He's a smart & imaginative guy so I started asking it of people I interview.

The interview process at virtually every company I have worked at has been 3 – 4 hours long. We ask basic programming, we ask brain teasers, we have them bring in some code they are proud of and spend 45 minutes having them take us through what they did and why. And 99% of the time, getting this question right or wrong predicts if they will pass the 3 – 4 hour process.

Question: For the integers between 0 and 100 (i.e. 1, 2, 3, …, 100) – how many have the digit 7 in them?

Answer at The Best Programming Interview Question – ever! (so you answer it yourself before seeing the answer).

share|improve this question

closed as not constructive by gnat, Walter, MichaelT, GlenH7, Frank Shearar Jun 13 '13 at 13:01

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. If this question can be reworded to fit the rules in the help center, please edit the question.

You could have used the spoiler markdown for the answer meta.stackexchange.com/questions/1191/… –  Conrad Frix Dec 17 '10 at 18:12
I might be able to come up with a few, but I wouldn't post them here. I'd be tipping my hand. All I'd realistically be able to tell is that they found the question on the Internet and memorized the answer. –  John Dec 17 '10 at 18:22
Why is that a good question? I just wrote out the numbers from 1 to 100, marked the ones where one digit is a 7, and then manually counted the marks. Got the right answer and took about 3 minutes. –  user16764 Dec 25 '11 at 1:16
Also, -1 because a) the example is so bad and b) this looks like an attempt to promote your blog. –  user16764 Dec 25 '11 at 2:31
The single weed out question is: "Explain why dynamic languages suck!" –  ThomasX Jan 20 '12 at 7:54

11 Answers 11

up vote 17 down vote accepted

This isn't really a brain teaser like the count of sevens example and it is a bit language specific, but I've been told by some that the ability to answer this says a lot for a developers ability to design large systems:

Explain the purpose of an abstract class, an interface, and what the difference between the two is.

share|improve this answer
But as you say, it is somewhat language specific, so does it mean that you can only design large systems in certain languages? :-) I guess I'd expect people to have some idea about the concept of interfaces (vs. classes, abstract or not) but seems like specifics would be less important. –  Guy Sirton Dec 24 '11 at 22:59
@GuySirton What language are abstract classes and interfaces specific to ? –  user61852 Jun 13 '13 at 13:10
@user61852: In C++ an abstract base class is a class with a pure virtual function. Those can and are used to specify interfaces (i.e. with no implementation and members). At any rate, my problem with this question is that it's mostly about semantics, not about concepts. The concept of an interface may be called an abstract class in some language while other languages have interfaces as part of the language. Any question where there can ambiguity about the terminology is a problem. I want to see if someone understand the concepts, not necessarily some particular language or terminology... –  Guy Sirton Jun 14 '13 at 1:07
+1 great example. I ask this question during phone screens and the average developer fails to answer it completely. Only the best candidates mention multiple inheritance. –  Brandon Mar 30 '14 at 18:06

I'm somewhat militant that interviews for programmers should always have a significant amount of white board coding. Here is the 3 part question I usually ask.

I am interested not necessarily in the right answer, but to understand how you approach problems and think. Therefore I want you to talk through what your doing and why as you write code.

This is a 3 part question. You may not use any string reverse builtins as this would make the question too simple. However, feel free to reuse/refactor the previous function you wrote if you feel it is applicable. In each case I will give you input and output and you should write a function that fulfills the criteria. In the spirit of the problem refrain from hardcoding the expected output as a return value.

a. input: "Hi my name is Bob"
output: "boB si eman ym iH"

b. input: "Hi my name is Bob"
output: "Bob is name my Hi"

c. input: "Hi my name is Bob"
output: "iH ym eman si boB"

I never write all 3 parts on the board at the same time. I always put them on one at a time after the previous one is answered and implemented.

I also often follow up each section with further questions about what to do if the their were additional requirements, how would you test it, anything else I think may be useful.

Obviously these are pretty trivial functions to write, however there are a few things that make this interesting and seems to give me a very good idea of the programmers capability.

  • I get a sense of their ability to communicate.
  • I get a sense of experience based on questions they ask, and the answers I get back.
  • It is a 3 part question because the last question is an just a call to the previous 2 functions written. Most people don't see that even with me reminding them after each part that they can reuse/refactor. The interesting part is every person that has seen the reuse solution has been a hire and an excellent programmer.

I think the questions are simple enough (possibly even too simple) that I rarely get someone who chokes because it's an interview environment. The choke and recovery test comes later. :)

share|improve this answer
IMO, string/word reversals are boring; it is ... a solved problem ;) –  Job Sep 8 '11 at 2:31
Bonus points for using a functional language and using the same function in all three cases... –  Matthew Flynn Sep 8 '11 at 3:23
a. reverse b. unwords . reverse . words c. unwords . map reverse . words . If I'm asked to implement reverse, I'll fold. –  Joey Adams Dec 25 '11 at 3:52
Anyone asking for string reversals, that doesn't specify ASCII strings, should be slapped, because an implementation that won't choke on all inputs isn't even remotely trivial, then if they specify ASCII they should also be slapped for using ASCII in 2012. –  whatsisname Jan 20 '12 at 0:40
@whatsisname: I usually say they're EBCDIC strings :) –  dietbuddha Jan 20 '12 at 0:52

I always ask fizzbuzz. My expectation is that anyone I would want to hire should be able to solve it almost as fast as they can write on a whiteboard. When I first started asking it I thought it would be a low bar that most candidates would ace before moving on, but I've found that even developers with years of experience can sit for 10 minutes or more and sometimes need help. I'm not looking for anything fancy either, just a correct algorithm, I'll even accept syntax errors. If I ever find someone who knows the answer already I'll count that as a pass.

For those that don't know, what I ask is this:

Using any language you want (even pseudocode), write a program or subroutine that prints the numbers from 1 to 100, each number on a line, except for every third number write "fizz", for every fifth number write "buzz", and if a number is divisible by both 3 and 5 write "fizzbuzz".

(I also ask Rob Z's question pretty much every time.)

share|improve this answer
As absurd as it sounds, you are completely correct that it is a useful filter. –  Adam Crossland Dec 17 '10 at 19:29
Huh. I'm surprised. I came up with a brute-force solution for that in less than a minute - just in my head, but enough to start talking and scribbling code as fast as I could. I'm guess that there is no "trick", right? Could that be the issue - the experienced devs knowing that most interview questions have a twist of some kind, and are confused that they can't see it? –  Ethel Evans Dec 17 '10 at 19:38
@Ethen Evens - I think part of the magic of fizzbuzz is that it can help check it someone has 10 years of experience, or 1 year of experience 10 times. –  rjzii Dec 17 '10 at 19:43
"If I ever find someone who knows the answer already I'll count that as a pass." But if they know the answer already, what have you shown? They probably know why a manhole cover is round as well. That's the problem with these kinds of well-known questions. With time they go stale. –  John Dec 17 '10 at 20:56
@John at CashCommons - Then change things up and ask them why Nashua, NH has triangular manhole covers. –  rjzii Dec 17 '10 at 21:05

"Please tell me about a challenging problem/design/system you solved or created."

"Tell me the story of how and why the challenge arose, and how you approached it. What were some of the requirements? (Requirements were discussed, right? And at least minimally documented?) What were the major architectural and design choices and trade-offs? (Architecture and design were explicitly considered, right?) What were the major data structures and algorithms used? How did you and/or your team manage change, bugs, and collaboration? What did you learn? What did you do differently after the project? etc."

"Please walk me through it and we'll chat as we go. Then I'll tell a coding story from my end too."

share|improve this answer
These questions are very good at picking holes in a candidates knowledge. I mean picking holes in a good way :) –  Richard Jan 19 '11 at 12:46
Thanks, I've found that starting this kind of open-ended technical discussion (and focusing on something in the past) helps both the candidate and myself to see how we learn from, and teach, others. Which would be a major activity on-the-job, much more so than solving puzzles under artificial conditions (high-stress, no computer, no shell to test, no web searching). –  limist Jan 19 '11 at 18:12

We always ask this question:

Write a power function (that is, "calculate x to the y")

Almost everyone uses a for loop at first.

Then we ask them to do it using recursion. Only a few of the folks get that, though we can usually talk them through it.

share|improve this answer
Any advantage of using recursion, or is it just for the exercise you ask that? –  Bjarke Freund-Hansen Jan 19 '11 at 13:00
@bjarkef: Recursion done properly (e.g., divide and conquer) is much faster. It naively requires O(y) multiplications, but recursive divide-and-conquer requires only O(lg y) multis. (Granted this is a slightly simplified as multiplication will not be an O(1) operation in the CPU for large #s.) Writing a quick python implementation, calculating 2^1000000 takes 66s with a for loop and with divide-and-conquer recursion takes only 0.02s. Sure you could write a slow recursion as well (def slow_power(b,e): if e==1: return b ; else: return b*slow_power(b, e-1) ) which wouldn't have any benefit. –  dr jimbob Jan 19 '11 at 20:25
To clarify more, it may be clearer with an example. If you calculate 216, you could do x = 1, then x*=2 for 16 times and get the answer for 16 multiplications. Or you could find recognize that 216 = (28)*(28), (28)=(24)*(24), (24)=(22)*(22), and 2**2 = 2*2. Thus you ultimately only need lg(16) = 4 multiplications with the recursive method (sure it could be implemented with a for loop but would be uglier/less natural). –  dr jimbob Jan 19 '11 at 20:43
@bjarkef -- We only ask for a recursive version for the exercise of it, to see if they can work it out, etc. Just the "Standard Harrassment Package". ;-) –  Nick Hodges Feb 1 '11 at 22:25
Those who read SICP will ace this question; others may take longer. –  Job Sep 8 '11 at 2:32

I would ask a question that had arisen over the course of the previous week(s) works.

For example, in the last month we've had internal discussions on Kadane's algorithm, and Kahan's algorithm ... I wouldn't expect a candidate to rattle those off the top of their heads, but I would be interested in presenting them the problem we had, and have a discussion with them about the solutions.

This always keeps your questions "fresh", and let's you see how the candidate would fit in with your team on a real problem.

share|improve this answer
Sounds like a good way to get some input, even from the candidates you do not hire. –  Bjarke Freund-Hansen Jan 19 '11 at 14:16

I will assume the question is for a software engineer/developer/programmer position.

I'm not going to post an specific question here. But my question would be more like a practical exercise in which I could include several topics like data structures, complexity analysis, out of the box thinking among others. So I would ask the candidate: Define an algorithm(maybe in pseudo-code) to solve X problem, and define which data structures you may use to solve it. Then argue why did you chose those data structure and what would be the complexity (Big-O notation) of your solution. And then discuss if he can improve the performance of his solution. In this way you can realize the way he thinks and solves problems and which techniques and approaches does he use.

share|improve this answer

One of my favorite questions was to ask to design a function for which

f(f(number)) == -number

is true.

There is a post on SO about it.

But such a questions sometimes become too popular. Like the question about to continue the sequence:


Basically I consider people's approaches to the problems they don't know how to solve. "I need to look at the documentation for operator precedence" or something similar is a good answer as well.

share|improve this answer
I think the last line you wrote out in that sequence should be "312211" :) –  Ethel Evans Dec 17 '10 at 20:02
@Ethel Indeed, thanks. Fixed. –  duros Dec 17 '10 at 20:03
The first time I was given that question I looked for a couple of seconds and then said that it struck me as a compression scheme gone horribly wrong. The person asking told me no which then sent me off in wrong directions for 10 minutes... –  David Thielen Dec 17 '10 at 23:16

We ask several questions, one of which is to write a routine to calculate n factorial. Looping and recursion are both acceptable answers, but the best hires we get are people who go straight to recursion.

One new graduate candidate asked me what kind of questions were going to come up. Rather than tell him our actual example I said that for example some people ask candidates to write some code to calculate all the prime numbers between 0 and n. He replied "oh right ... what's a prime number?". We didn't hire that one.

share|improve this answer
Why are the best developers the ones who go straight to recursion? If you have a large number (> 1,000,000), there is the possibility of a stackoverflow, where iteration doesn't. If they give you the iteration solution and then say "I have done this because recursion has performance risks, but here it is anyway", surely they are a better programmer than a recursion robot –  Richard Jan 19 '11 at 12:45
@Richard, It depends if the language has tail recursion. –  dan_waterworth Dec 24 '11 at 21:23

I always ask some questions that move away from the normal "code Fibonacci" or something that they will already know. I like to ask questions that show an indepth knowledge of the underlying language and an understanding of computer science.

Once of my questions (which people keep saying is too harsh) is: you have two integer values and you need to swap them without using a temporary variable. How do you do it?

XOR Swap

I know people will say that it isn't used and people shouldn't know it, but I think it shows a person knows about their subject if they do know it. Plus, if they don't, I will lead them to using the XOR operator and see if they can work it out from there. This again shows a logical thought process and a knowledge of Computer Science.

After that, we can discuss any performance problems for bonus points.

share|improve this answer
does using assembler and using a CPU register counts as "temp variable"? –  Quamis Jan 19 '11 at 16:02
also, using shift and AND instructions would be considered ok? –  Quamis Jan 19 '11 at 16:03
There are other ways to do it. One of which starts by multiplying or adding the two values. –  user16764 Sep 8 '11 at 5:27
Some modern languages (like python) allow this: a, b = b, a would that count as an answer? –  gnur Sep 8 '11 at 10:27
I would think that any answer would be valid if it was correct. I didn't know Python could do that though, thanks for the tip :) –  Richard Sep 8 '11 at 10:58

When was the last time you designed something that used immutable data, and why?

If I was going to hire someone, I'd want them to be already be in the habit of thinking functionally; I'm opinionated that way.

share|improve this answer
every time I use a string in C#. –  stephenbayer Jul 22 '14 at 20:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.