A favourite interview question
Today I interviewed a candidate for an internship on my team. One thing I’ve learned from when I conducted screening interviews for potential Extreme Blue candidates at IBM was that no matter how well a candidate answers theory questions, you still don’t know if they code. As an interview, you must ask the candidate to write some code. Given that it’s an interview, and the candidate is sure to be nervous as hell, I picked a simple question:
Write a function (in Java) that takes as arguments a string and a char, and returns the last index of the specified character in the string.
It sounds simple enough, but believe me I’ve seen more than my fair share of failures and embarrassments on this question. It’s like FizzBuzz.
Some of the things I look for in an answer:
- Did the candidate ask what to do if the character isn’t found in the string at all? (In which case I tell him to return -1, but I don’t volunteer this information until he asks).
- Did he search the string from the end instead of from the beginning?
- Does he handle the case of a null or empty string?
- Is his code clear and easy to read?
I’m always surprised at how many times one, two or all of those criteria are missed.


Do you specify that you can’t use any functions?
Otherwise why wouldn’t they just write something like this:
function findLastIndex( String stringParam, int ch) { int retVal = -1;
} return retVal; }
Yes, I forgot to mention it in the post (good catch) but I explicitly disallow lastIndexOf or indexOf. (Btw, your example will explode if stringParam is null).
I’d personally not check for null, but I would clearly state that the function assumes as a precondition that the input must be a valid, none-null string. DbC ftfw. Saves a check for null, promotes dynamic programming, shows you’re smart and leaves the bugs at the caller’s side. So long as you’re (I’m) clear about the preconditions.
As for starting the search at the end of the string, I fear I might fall prey for rushing into coding and immediately start at the beginning. But I have no doubt I’d see this soon after I (start to) code.
I think I’d ask about the char-not-found case sooner rather than later. I’d probably also ask about integer overflows, should the string be too long. Java strings aren’t null-terminated, but it’s worth mentioning the case where the char is null. (Shh, I just googled to settle my in-confidence about the end of a string in Java, but don’t tell anyone). I’d also mention the case of multibyte or variable-width string encoding, even though Java supposedly takes care of this for you. I just don’t like to be spoon-fed, so I like being conscious of these things.
I’d mention my code is thread-safe in allowing multiple threads to operate on the same string, so long as the string is not modified by some other function outside the scope of the threads. I’d mention the function poses a no-throw exception safety.
The case of an empty string is the exact same case as that where the char can’t be found.
All this and more in my wild, wet dreams where I actually do well when people watch and judge me. Alas, I am widely known for FAILing kindergarten-anything when I’m nervous.
A quick microsoft interview I had on campus they asked essentially the same question (except it was just index of, not last index of).
I must admit, being nervous seriously fucks with your ability to code properly, even something simple.
Turns out you are right..I feel like I used to use this condition before and it worked. Maybe I was using StringUtils.isBlank(). This just goes to show you what happens when you stop coding for a while.
I’m gonna cash in on that beer, then. https://twitter.com/benbaril/status/691751710892032
I think you’re going into too much detail with a lot of those points (your C++ background is showing ;-)), for a toy problem on an interview anyway.
Mentioning that the string is assumed not to be null is ok, it shows that you thought of it and that’s what’s really important. Even better would be throwing an IllegalArgumentException or making the function return -1.
If someone started from the beginning of the string the first time (most people do) then I would expect them to notice that they could/should start at the end, or I would ask them to walk through an example with a super long string and see if they figure it out.
Strings are immutable in Java, and chars are passed by value, so your function would be thread safe by default. And yes, ALL strings in Java are UTF-16 so they are double-wide and you don’t have to think about it. Also, you cannot get an integer overflow from a string length; Java strings can’t be that long.
I’m glad I wasn’t asked any technical questions in my interview because I can’t code anything without intellisense :(