Job Interviews for Java Developers: Attention to Detail

Someone has read my post about interviewing where I wrote about String reversal, but got it totally wrong. Ironic. He didn’t show much attention to detail, took quotes out of context, put things into my mouth that I’ve never said and concluded “Want a job? Reverse a string. The sad state of job interviews today.” This post even got to DZone. So my post could also be titled: The sad state of DZone today.

Most of my interviews are 1h to 1.5h and sometimes 15 minutes of the interview is about reversing a String. This is mainly to get into the thinking of the candidate, see how he deals with fringe cases (null values as arguments) and if he chooses a JDK reverse method or not. Jonethen didn’t pay much attention to detail, so for a starter he got my name wrong, it’s Stephan not Stephen. A minor detail but a sign for what was to come later in his post and the comments.

Jonathan is angered by my mentioning of the recursive version of reversing a string. Although I wrote why I think it’s interesting to discuss a recursive solution, he ignores my arguments and writes

“.. only that he would be much more impressed if someone did write it out.”

I didn’t say that.

“He would probably give higher preference to someone who did use that technique,…”

No I won’t and I didn’t say that either. I said “The best implementation in Java is to use the reverse method of the StringBuffer class in the JDK.” I specifically pointed out that the reverse version is impractical in Java for the immutable String class alone, not to mention the surrogate pairs problem. So to make myself clear again, the best solution is to use the reverse method of the StringBuilder class. Any candidate who uses this method has already impressed me, because from my experience very very few developer know this. Most would write some kind of array swap method. But it’s not the cause I ask this question. It’s to see if someone can write code (which doesn’t need to be compiler ready, he’s no IDE in the end) and get into a discussion about programming, unit testing, fringe cases, how to handle errors, quality assurance and other topics. And if someone has read this blog? Or another blog? And knows the answer? Well good, the answer is just the beginning. And considering that very few developers read blogs and articles to learn something new, I’m happy to interview a candidate who reads blogs in general.

Without getting personal, I look for more attention to detail in the candidates I have than Jonathan did when he did shred my post. And I look with far more attention to detail into candidates than Jonathan did use when reading my post. Ironic.

One poster in the comments of Jonathans post suggested using StringBuilder instead of StringBuffer, because StringBuilder is not synchronized and faster therefore. I think modern VMs can use escape analysis to detect synchronized blocks or methods which can be optimized away, as in the local usage of StringBuffers. There should be no difference between StringBuffers and StringBuilders in modern Java VMs. But it doesn’t hurt to use StringBuilder.

Ending his article: “If a interviewer wants to talk about my past projects with me, then it should not take him long to realize that I am a competent.” Or read a blog, as Jonthan suggested in Blogging gets people hired.

Hopefully he doesn’t read your blog.


PS: I wish I don’t sound the least bit like Hani ;-)

Cool things: Automatic Online Interview Questions with Janino

Lately I wrote about interview questions, specifically about asking how to write a String reverser method. When searching for a job as a PR-consultant my girl friend was sent a PDF to answer questions – which I guess the company uses to gather fresh ideas – and she had to send the answers back written in the PDF. This got me thinking.

If you have lots of candidates for a programming job, screening them online might safe you some time. You could ask the candidates to write some code, for example “to reverse a String”. How do you determine if the answer is correct? Write a unit test. How do we execute code the candidate entered into a form? Just use a compiler. Janino is a Java compiler which can compile Java source code that it reads from a String:

You present an input form, protected with a token you’ve sent the candidate. Making the job easier, you can provide a syntax highlighting editor like codepress. When he enters the code, Janino compiles it, runs a JUnit or TestNG test and tells the candidate if his code is correct or not. Obviously people will cut and paste code into the box when you ask for the answer to a simple problem like string reversal (Google is just one click away). But asking the candidate a more tricky question, which he cannot google might work. Like ask them to write an elevator simulation (state machine with several elevators) and test the result with a unit test.

Even better you could also make them write code to satisfy a Fitnesse story. With fitness it’s easy to add further constraints to the test and you can ask the candidate to extend the test with constraints he considers necessary. Make him write his tests too ;-)

The nice thing with Java, compared to Ruby and PHP, is that Java has a fine grained security concept. With a Java security manager you can make this application safe by preventing access to file and system resoures and still execute the code the candidate entered into the form. The same goes for other languages on top of the VM. So for asking Ruby questions you can use JRuby.

Hope this gives you some ideas about asking interview questions online.

Thanks for listening.

Java Interview questions: Write a String Reverser (and use Recursion!)

Interviewing developers for a programming job is hard and tedious. There are some excellent Guides, like the Joel Guerilla Guide to interviewing, but in the end you need to decide yourself to hire or not to hire. To get a quick idea about their programming abilities I have considered asking the String reverse question. Others have used this question with some success. There are lots of answers to this question which gives you room to explore the candidates skills. Thinking about this myself, I found some answers on how to reverse a String in Java. The answer the candidate gives is a good way to see how he thinks. You could combine this question with one about interfaces and ask for a reverser interface:

The best implementation in Java is to use the reverse method of the StringBuffer class in the JDK. It’s fast, efficient and knows how to handle unicode surrogate pairs, something most other solutions ignore.

Not only is the chosen implementation interesting as an answer, but also does the candidate reuse the JDK or not or does he tell you at least “there has to be something in the JDK”. Which is quite as good, because googling in reality will help him find the JDK solution. You don’t want developers to implement everything themselves.

Handling problems

Ask him where the bug is in this code, even if there is none. Or how his code can go wrong. His answers can lead into a discussion about how to handle a null value

  • return null
  • return “”
  • throw NullPointerException
  • throw IllegalArgumentException

and the merits of every solution. The second discussion point is how to optimize the solution, like returning the string itself for “” and every one length string (which is a reversal of itself already).


Then ask the candidate to write a recursive solution of the reversal problem (which is the most beautiful but least usable one).

Some developers can’t handle recursion in their head. Most candidates will need some time and some help, but actually get to a solution. Those that can’t handle several stages of recursion in their head probably can’t handle complex problems or several layers in their head either.

You can ask them about the efficiency of the recursive solution, ask about Tail Recursion, ask about the ineffeciency of the “+” operation for Strings, how to handle that, about why Strings are immutable (most of the time at least) and ask the candidate how many String objects are created for reversing “Stephan” with his recursive solution. When discussing this one of my developers said “Easy”, he was doing Lisp at the university the whole time, which I didn’t know until then, excellent news!

Ask where the stop condition is in the above code to end the recursion.

More solutions

Some more solutions, one with swapping a StringBuffer in place:

One with swapping an array:

and one with appending to a StringBuffer:

Perhaps the candidate even knows about the tricky XOR swapping solution.

From there it’s an open field. You could ask the candidate to write a JUnit test for his reverse method. Not only can he show how to write a unit test, but what he considers as test cases (“”, null, “A”, “Even”, “Odd”, ….).

I hope this helps you in your decision with hire or no hire. And I hope it helps me in the future to decide this question for myself. As Joel said: when in doubt, always no hire.

Thanks for listening.

See also: