the blog for developers

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:

import org.codehaus.janino.SimpleCompiler;
import java.io.StringReader;

public class Evaluator {
  public static void main(String[] args) {
    String source =
        "import java.util.*;\\n" +
        "public class MyReverser implements Reverser {\\n" +
        "  public String reverse(String str) {\\n" +
        "    return new StringBuilder(str).reverse().toString();\\n" +
        "  }\\n" +
        "}";
    try {
      SimpleCompiler compiler =
         new SimpleCompiler("Test", new StringReader(source));
      Reverser reverser =
         (Reverser) compiler.getClassLoader().loadClass("MyReverser").newInstance();
      System.out.println(reverser.reverse("Stephan"));
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

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.

You can leave a Reply here. Of course, you should follow me on twitter here.

You can share this post!
Do you want to tell others about this article? Use the social bookmark icons to submit this artice to the service of your choice. Thanks.

About the author: Stephan Schmidt is head of development at brands4friends. He has more than 15 years of internet technology experience and 10 years experience in agile. He was head of development, consultant and CTO and is a speaker, author and blog writer. He specializes in organizing and optimizing software development helping companies by increasing productivity with lean software development and agile methodologies. Want to know more? All views are only his own.
Leave a reply.

Comments

It’s an interesting idea but instead of using Janino a simple custom class loader that would compile, load the class and run it against the unit tests would do, no?

While in the university there was a similar idea in one of the courses about algorithms. We would upload the source code (which was C not Java) and they would compile and run against a few tests that would then tell us if the code was correct and within the stipulated performance boundaries.

The only problem I see in these sort of “tests” is that you have no proof that it was actually the candidate to answer your questions. It probably will help you screen out a few candidates, but most of the work will still be done while reading CVs and interviewing… Don’t you think? (I never recruited people so my point of view might be a bit different from reality)

stephan

What simple classloader?

You need a compiler to get the source to byte code, whereever the compiler is. And one of the simpliest compilers to use is Janino.

“The only problem I see in these sort of “tests” is that you have no proof that it was actually the candidate to answer your questions.”

As I said, you need a specific problem to solve, which he can’t find online for this to work. Perhaps a small percentage will ask a friend to solve the problem for him, but as this online questionaire is only the first step, you can judge later on if he’s a hire.

Yes most of the work will be done while interviewing.

What simple classloader?

A simple custom classloader, which would load the string, compile it generating the bytecode and finally loading the class into memory. I know that’s what Janino does…but I was just stating that you could do the same by simply creating your own custom class loader.

stephan

@Paulo: “…but I was just stating that you could do the same by simply creating your own custom class loader.” And write a Java compiler on the go? Then I’d rather use Janino. I’m not sure if the Java 6 compiler API would also solve the problem.

Janino has such a class loader btw :-)

http://www.janino.net/use.html#class_loader

martind

I may be stuck on a tangent here but — uh. pls don’t do that. either send them unit tests so they can run it locally while developing their solution; or make sure your compiled code is only executed in a heavily locked down sandbox. but don’t just allow people to post code and then blindly execute it on your machine.

:)

stephan

As I said, “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”

:-)

Nice.
I like it!

Leave a Reply

What people wrote somewhere else:

Additional comments powered by BackType