Swipe left or right on code? Next killer app for code reviews? I won’t jump right into development for that one. But, check this out:
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
System.out.println(i);
}
}
Let me rephrase my question: what do you think about this code? And, pretend the person asking the question is an interviewer.
Some possible answers:
- Can you be more specific?
- Looks good to me.
- Well, it’s a for loop that start counting from 0 and iterates 10 times before finishing. In each iteration, it checks if the counter variable i is even. And, if so, i is output to standard out.
Let’s be honest. Answer #2 is horrible. Hopefully, you picked up on that. Answer #1 is the best one. The interviewer might want you to simply explain how the code works and be content with that. They may right want to know if you can identify the programming language as Java. Or, maybe they just want to see what assumptions you make and what questions you ask. But, if you don’t ask for clarification on such a broad scoped question, you’re only guessing.
One of the primary rules of technical interviewing is never make assumptions without calling them out. If you make an assumption and go down a path of thinking completely outside the space of what your interviewer is looking for, you run the risk of making mistakes and striking out. Not only that, but you miss out on the critically important opportunity to demonstrate to your interviewer that you’re someone that doesn’t make assumptions—or makes them and clarifies them—and is ready to collaboratively problem solve.
Maybe in response to your follow-up question, they’ll say, “does it need any improvement? If so, how would you re-write it?” And, they might be looking for something simplified, self-documenting, and testable like this:
public void printEvenIntegers(PrintStream outputStream)
{
for (int evenNumber = 0; evenNumber < 10; evenNumber+=2) {
outputStream.println(evenNumber);
}
}
// example call
// printEventIntegers(System.out);
Don’t be shy about asking questions during an interview. Your ability to ask questions is part of your assessment.