The Code Sample (The Programmer’s Guide to Getting Hired)


Why you are being asked for a code sample and what it says about the employer.

At some point during the developer recruiting process, any hiring manager with the remotest concept of due diligence is going to attempt to get a preview of what to expect from the applicant either by requesting a code sample or asking them to write some code during an interview. In furtherance of my ambition to minimize my resemblance to a jackass and to do my part to optimize useless effort out of the universe, I tend to forestall asking for code samples until I have arrived at the short-list of candidates that I would seriously considering hiring. Unless I have more than five or six really solid candidates (very rare), I’ll generally ask for the code sample at the end of the phone interview if it has gone well, and ask the candidate to e-mail it or bring it with them to the in-person interview.

Although management styles differ, I think it is a safe bet that a code sample request is a strong indicator that you are progressing well through the recruitment process and so far have made a favorable impression. I base this on the assertion that few managers are going to be willing to wade through dozens of code samples from marginal candidates in hopes of finding a diamond in the rough unless they are having a really hard time finding candidates because they have a lot of positions to fill quickly or need someone competent in a niche technology.

It’s a good thing, I promise!

I definitely understand that competency tests like this during the recruiting process are stress inducing for the candidate, but take heart that a request for a code sample can be a very positive sign for the candidate. Not only is it a sign of interest, but it also shows that the company is the type of place that carefully vets their technical people. Believe me, you do not want to work somewhere with lax recruiting standards. There are a number of reasons that companies hire without scrutinizing candidates thoroughly and almost all of them are bad news the person who ultimately gets the job.

  • They have a backlog on recruiting due to high turnover as they desperately try to replace people who are running out the door.
  • Lazy hiring managers who would rather over-hire and then weed people out on the job.
  • Supervisors that don’t understand technology and are afraid to ask questions because they wouldn’t be able to interpret the answers.
  • Lots of under qualified co-workers who are a continuing source of bad code that you’ll have to debug, and no mentors in sight.

Dissenting opinions and my rebuttal

If you have spent any time in message forums that focus on job-hunting for techies, you have no doubt come across a certain vociferous element that despises employers who request code samples and/or certain types of interview questions. While I empathize on the abuse of puzzle questions, extremely obscure technical questions, and extreme pressure interviewing techniques; the arrogance exhibited by some of these people frankly amazes me. In a separate article I discuss to the type of candidate that thinks “I’d just Google it.” is an acceptable answer to any technical interview question, but for now I’ll to address the fallacy of arguments like the following:

“Can you believe they asked me for a code sample? I have 10 years of development experience on my resume and four certifications. I’ve earned my stripes and shouldn’t need to prove myself to anyone. They are probably just trying to trick me into doing work for free. I’m not a monkey who is going to dance for their amusement!”

Unless this guy is Bjarne Strousup, John Carmack, or some other luminary of technology, he is wrong. Professional programming is the type of gig that has very few celebrities. If I could identify the most skilled programmer alive today, chances are that you wouldn’t recognize him or her. Even for those few programmers that you might recognize either their name or work, it will often be only because of the high profile projects they worked on and not necessarily their code-fu. The simple truth is this, most programmers are anonymous, even the really good ones. Before you start commiserating with complainers such as the one in my example, be sure to read some of his other posts about how there are simply no jobs out there evidenced by the fact that, despite his obvious skill, he has been out of work for 8 months already. Recruiters can spot attitude problems like this a mile away and know enough to put a second mile between themselves and these head-cases for good measure.

The bottom line is that you shouldn’t take efforts to validate your qualifications personally. Hiring managers have just learned through hard experience that they can’t blindly trust anything on a resume. There are simply too many people applying for jobs with puffed up resumes full of certifications yet they couldn’t code their way out of class PaperBag:Bag. It’s just the process, not an impeachment of your abilities. If you are still not convinced, consider what you would assume about a supposed “rock star programmer” who was so hesitant to show off his code. It smells fishy, and managers don’t want fishy when making decisions that will cost 5-6 figures per year plus benefits.

Code sample requests encourage “failing-early” in the recruiting process

Before I even look at the code sample provided by a candidate, I have effectively screened out three types of undesirable candidates that won’t submit the requested sample.

  1. The angry egoist described above.
  2. The resume padding exaggerator with no programming skill who has just realized that I have called their bluff.
  3. The tire kicker, who isn’t really interested in the position, but is applying on the off-chance that I might be able to give them a $50K salary bump.

The hidden agenda

The request for a code sample has a side benefit, in that it lets me test something beyond programming skill without being completely overt about it. In many cases I’ll add some special requirements on the code sample request, for example specific languages, length limitations, or submission instructions. This is an excellent way to detect candidates who refuse to follow directions or jump into solving problems before attempting to understand the requirements. Closely reading the instructions presented by a prospective employer, asking relevant questions as needed, and re-checking against those instructions before submitting will get you further than at least 25% of the candidates that make it to this stage.

 

No excuses – give up the sample

There are a number of reasons people give for providing a code sample. In my opinion, none of them are acceptable. I’ll address the two most common here.

“All my work related code is proprietary. I can’t betray my current/former employer’s trust by showing it to you.”
Good for you. I definitely respect your dedication towards protecting your employer’s intellectual property. In fact, it would probably color my opinion negatively of you as a candidate if you submitted a code sample that looked like it might contain trade secrets. However, that doesn’t get you off the hook. Many candidates mistakenly presume an implicit requirement that the code sample must come from your professional work experience. Unless you were specifically told this, this assumption is usually not valid. Whether it is fair or not, I interpret these responses as “I can’t send you any of my existing code, and I’m not interested in the job enough to bother writing anything new.” Do I really want to hire someone with the attitude, “I have to write code? Bummer!”

“I am a student and only have code from academic work.”
Again, I don’t care why you wrote it as long as it is your own work and it meets any requirements I specify.

 

Even if you do have some previous professional work that you are not restricted from sharing, I’d strongly advise you to take the time to write something fresh to use while you are interviewing for a programming job for a number of reasons:

  • Your coding skills should be improving over time, thus your newest code should be the best you have ever written.
  • The code you have already written was probably created under constraints that won’t be apparent to the hiring manager. You don’t want to submit code that requires an explanation or apology.
  • In most cases, you are free to write code to do whatever you want for the sample. You rarely get a chance to choose a problem that you are good at solving, take advantage of it.
  • Your existing code was written to solve a work problem not sell your skills. Different objectives require different approaches.
  • You have a chance to write code that matches the technologies/tasks that match the job description.

That last point is important and applies to other areas of the recruiting process. Anything makes you seem more familiar to them will help your chances because it encourages them to start thinking of you as an insider. Unfortunately, I also think this tendency is also the source of a lot of unintentional and illegal discrimination during the hiring process, but good managers have learned to recognize their own natural biases and find ways to remain objective about candidates.

Code sample ideas

For the truly stumped, here are a few ideas for programming tasks that can be solved in just about the right amount of code that works for a code sample, yet aren’t so trivial that they undermine your credibility.

  • A utility class for logging events and errors either to a file or the system event logs with a configurable level of verbosity.
  • A set of stored procedures for maintaining and searching data in a hierarchical tree structure.
  • An implementation of the singleton design pattern to serialize access to a resource file.
  • A calculator function that takes a single string of input that includes numbers and operators and returns the numeric result of the equation. Don’t forget input validation, error handling (divide by zero), and operator precedence concerns!
  • A method that takes a URL to a web site and a local path and downloads all image files based on <IMG> tags.
  • Create a method that takes a keyword, message, and login credentials and uses the Twitter API to respond to post the message as response to any friends of the account specified.

 

Best practices for preparing a code sample

A request for a code sample is like a take home exam, and there is no excuse to not ace it. Although code samples are more frequently used to identify potential problems than recognize programming skill, there is still some opportunity to score points with your submission. Making sure the code is highly readable is one of the best things you can do to improve your standing through the code sample. Although some of these items should not be considered a best practice for actual programming work and many of them are not even practical when you don’t have total control of the problem you are trying to solve, they are great ways to increase the readability of code sample submissions:

  • Separate logical blocks of the code with extra blank line. Whitespace is your friend.
  • Avoid line-wrapping as much as possible.
  • Method and variable names need to be meaningful at face value without reading the code that calls or implements it.
  • Reduce the number of parameters to functions you write, and avoid passing in optional parameters as much as possible.
  • Variables should be single use only.

Before
Dim loopCounter as integer
For loopCounter = 1 to maxPages
‘do something with pages
Next

For loopCounter = 1 to maxDucks
    ‘do something with ducks
Next

After
Dim PageCounter as integer
For PageCounter = 1 to maxPages
    ‘do something with pages
Next

Dim DuckCounter as integer
For DuckCounter = 1 to maxDucks
    ‘do something with ducks
Next

  • Use variables instead of inline function calls

Before
fooResult = foo(subfoo());

After
subFooResult=subfoo();
fooResult = foo(subFooResult);

  • Put in at least a few comments to show that you know that you are supposed to. However, follow best practices and don’t overdo it.
  • Show the code to another programmer and ask them to tell you what it does and how as quickly as they can. If you get even the slightest urge to explain while they are reading or they ask questions, rework it to make it self explanatory.

Don’t forget the quality control

You did have five of your best friends QC your resume and cover letter before you submitted it, right? Isn’t the code sample worth the same scrutiny? Understand that the code sample is used more often as a NEGATIVE differentiator than a positive one. In case you don’t speak marketing, this means that the person reading your code sample is using it to weed out bad candidates not find good ones. Here are some suggestions to keep your resume out of the trash-bin:

  • Compile the code – It is just plain lazy to give them code with mistakes that are so easily caught.
  • Spell check your comments and identifier names – Yes it is silly, but don’t give them chances to take cheap shots at you.
  • Unit test the code thoroughly to make absolutely sure it does what it is supposed to. If the job posting lists TDD you might even consider submitting the unit tests for extra credit, but be sure to confirm before doing this if it forces you over the code size they asked for.
  • Check for and eliminate any code that is borrowed or even looks like it might be. Hiring managers sometimes Google unusual identifiers and method names to make sure exact matches don’t come up for your code. You might want to do this too to see what comes up.
  • Find the smartest programmer(s) you know and ask them to tell you 5 problems, no matter how nitpicky. This prevents them from trying to be polite or lazy and not looking too closely. Getting the job is more important here than protecting your ego.

Other tips on preparing a code sample

 

  • Keep it short and simple. They are looking for ammo to filter you out. Don’t give them a lot of code to find fault in.
  • Don’t use clever coding tricks. They decrease readability and rarely impress anyone.
  • Use the code sample to demonstrate your knowledge of technique (Design patterns, generics, OOP, TDD) and specific technologies (AJAX, Web Services, XSLT, SQL DMO). Use the job description for cues for what things you might want to include.
  • Avoid controversial approaches such as Hungarian notation whenever possible. You don’t want to be forced into a confrontational posture with a zealot from a different camp that has pull on whether you get the job.
  • Don’t use comments to plead your case or apologize to the hiring manager as shown in the following counter-examples:
    /* I am really good with OOP as shown in this class! */
    /* Normally I would add error handling here if this were production code, but since I’m not taking this seriously… */

Your code sample and the interview

Even if you get a callback for an in-person interview after submitting your code sample, don’t rest on your laurels yet. For managers that work with a system similar to my own, there isn’t usually a round of cuts between receiving the code sample and the formal interview. Usually I just briefly scan the code sample for obvious deal-killers and don’t really read them carefully until the day of the interview so the candidate has a chance to defend it, if necessary. You can and should expect the code sample to be a topic during the interview, so be prepared.

Know thy code: You should know the code you submitted backwards and forwards, have some ideas about other approaches you could have taken, be ready to optimize it, and talk intelligently about all of the technologies you used. The code sample is fodder for under-prepared interviewers who are digging for something to ask about. “Interesting, you used the Fizbah protocol here. Would that work over IP too?”

Defend thy code: A lot of interviewers like to use a technique that involves contradicting the candidate in some way, such as questioning the approach in a code sample. You absolutely must defend your approach unless the interviewer has pointed out an indisputably better or more correct approach. Do not concede the point unless you understand the counterpoint thoroughly. A common trap is for an interviewer to play devil’s advocate just to see if you will fold under pressure. For example you are sunk if you bite when the interviewer makes a claim he knows is false, like “You shouldn’t have used the StringBuilder class here, it doesn’t work with Unicode.”

…but not dogmatically: When defending your code, however, you have to be careful to not give the impression that you are closed-minded or that you think you know everything. For example, if you got the StringBuilder/Unicode question and weren’t sure if the interviewer had a good point, you might say something like “Are you sure about that? I am pretty sure that isn’t the case, but it is a very valid concern. I’ll double check the documentation and follow-up with you in an e-mail later this afternoon.” This answer helps dissuade the notion that you might be a pushover, shows that you care enough in your craft to dig a little deeper, and that you are the type who likes to share knowledge of new discoveries.

…, and show that you know how to listen: When an interviewer criticizes a code sample or an answer to a technical question, many candidates turn off their ears as soon as they get detect a negative tone and start to cogitate on their rebuttal. Unfortunately this often causes them to completely miss the subtle clues that interviewers drop to help the candidate navigate the objection. It is critical that you practice active listening skills during an interview for this very reason. It looks bad when you make mistakes in an interview, but it is far worse to continue to flounder on a technical issue when the interviewer has thrown you a lifeline and you didn’t recognize it.

About these ads

14 Responses

  1. On the second code example, I believe it should be:

    After
    subFooResult = subfoo();
    fooResult = foo(subFooResult);

    • I believe you are correct. I have fixed the example.
      Thanks for pointing that out.

      • This is funny actually. You could have spent days writing this article for all we know and you got your own code sample wrong. So what do you expect from an applicant during the stress of a 45 minute interview?

        You just disproved your own point. Coding questions during an interview are ludicrous. Ask for some open source code instead.

        • First, this article isn’t really about writing code in an interview. Usually when an interviewer asks for a code sample they do it like a homework assignment. Writing code in an interview is a whole other topic.

          Second, I really appreciate your faith in my skills that my bungled code snippet invalidates code samples as a way to evaluate programmers.

          Finally, when evaluating a code sample it is important to know the difference between a typo/honest mistake and a fundamental lack of technique and understanding.

          As to your point about asking for open source code. You might want to re-read what I was saying. The whole point of this article is advice to the candidate when presented with an open ended request for a code sample. It was a response to a question I saw on StackOverflow from an interviewee who got such a request and didn’t know what to offer.

          I do appreciate your feedback through and think it is good policy to embrace your critics. Thanks for commenting!

  2. I think the bit about “I would just google that” is controversial. While I would want to hire someone that can write code, I would not want them to waste time re-inventing the wheel. If there is already a library that handles a needed function and it can be found by Google, then I would want the Software Engineer to use that. He just saved the company time and money and probably the library is better written and more debugged then my programmer would have time to do himself.

    • You make a very valid point, but understand the context of that statement. I have no issue at all with developers using Google as a resource on the job. What I am referring to here is the candidate who throws this out whenever they can’t answer a technical interview question. If I were asking silly interview questions like “How many overloads are there for the .NET RegEx constructor that are available on the compact framework? ” then I’d deserve that kind of response. However, I tend to ask more situational questions and provide a lot of leeway for syntax/method names for any whiteboard code written during interviews.

      Perhaps this example explains better what I am talking about:
      Interviewer: Performance problems have been reported in your 3-tier web application, describe your general approach for tracking down the source of the performance issues.
      Candidate: Uhhhh. I’d Google it!
      Interviewer: Google what, exactly?
      Candidate: “Performance optimization”
      Interviewer: Thanks for your time…

      In any event, the “I’d Google it” answer really never satisfies any question that is asked in an interview because it doesn’t tell the interviewer anything about the candidate that differentiates them, unless they have exclusive access to Google or something.

  3. The best code sample is your OSS work. Every position I’ve been offered in the past 7 years (and I’ve been offered dozens in that time) has mentioned my code on CPAN as a factor for offering me the job. Sometimes, these job offers have been cold offers – I didn’t even know I was being evaluated.

  4. You mentioned avoiding certain things like Hungarian notation (Apps or Systems?) to avoid getting dinged by someone who zealously follows a different methodology. I wouldn’t necessarily consider that result a bad thing. It may cost me the job, but then it’s costing me a job with a difficult person in a position of influence. Am I worse off? Sometimes, but not always. Even if it doesn’t cost me the job, it gives me information I wouldn’t have had otherwise. If you’re highly confident of your skills and in a strong position, you might consider intentionally putting in a few tripwires like that, as long as they are truly subjective matters of taste.

    • You make an interesting point. However consider the following:

      1) For the purposes of the article, I’m assuming you really want the job and aren’t screening the company using the code sample. It’s an interesting idea, but that is a whole other topic.

      2) More often than not, you are never going to know what they thought of your code sample when they rule you out. So the learning potential is probably very limited.

      more often than not,

  5. [...] Posts Programmers: Before you turn 40, get a plan BWhat Programmers Should Be Learning in CollegeThe Code Sample (The Programmer's Guide to Getting Hired)Database Smells, Redneck [...]

  6. [...] The Code Sample [...]

  7. [...] code samples of previous [...]

  8. Great Article

    Thanks for the help

  9. this was very insightful…i’m looking for a new job and I’v been asked to provide some code samples and I’ve been at a loss…Though this is a bit outdated, the concepts are great. Thank you!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 128 other followers

%d bloggers like this: