I haven't been asked this question, either.
At first glance, it seems like one of those "weed the obviously incompetent idiots out as early as possible to avaid wasting valuable interview time" type of questions.
But if you look at it more closely, there's actually some quite interesting stuff in there. So, if I were the one asking that question, here's what I would be looking for:
- That question is obviously stupid, because there already is a function in the ECMAScript standard library that does exactly that. And I would want the interviewee to tell me that the question is stupid, because otherwise they are either a) mindless zombies that stupidly follow braindead orders instead of engaging their brain, or b) they don't actually know that that function exists.
- It's also obviously a parsing problem, and it is interesting to see whether the interviewee approaches it as more of a string hacking problem or a formal parsing problem and how much overhead either approach generates. In this particular case, I believe that string hacking is the right approach, but it still leads into a great followup question: "Now do the same thing with a recursive-descent parser". Any programmer should be able to sketch out the recursive-descent parser for this problem within a couple of minutes.
- Last but not least, this is obviously a fold over the characters of the string. Now, I would not necessarily expect a novice programmer to spot this fold on their own right away, but if I hint that there is a fold in there, they should be able to spot it themselves, and rewrite their solution in form of a fold.
- And of course, you can judge all the general qualities that this type of question allows you to: does the interviewee stop and think about the problem or does he start hacking away. Does he start with requirements, documentation, specification, examples, tests, or code. Does he ask for clarification of the corner cases (like what happens with the empty string, what happens with a string that only contains a minus sign and nothing else, what about whitespace, are strings guaranteed to be well-formed integers, is negative zero a well-formed integer). Does he habitually use the strict subset of ES5. Does he write readable code. Does he write
jslint
-friendly code
Here's an example of solving the problem with a fold (which in ECMAScript is called reduce
):
"use strict";
function toInteger(s) {
return s.split('').reverse().reduce(function (n, c, i) {
if (c === '-') return -n;
return n + (c.charCodeAt(0) - 48) * Math.pow(10, i);
}, 0);
}
And this is a simple recursive-descent parser which builds up the value on the fly:
"use strict";
function toInteger(s) {
var input,
output = 0,
sign = 1,
lookahead = function () {
return input.charAt(0);
},
consume = function () {
var res = input.slice(0, 1);
input = input.slice(1, input.length);
return res;
},
isDigit = function (c) {
return /[0-9]/.test(c);
},
signParser = function () {
if (lookahead() === '-') {
sign *= -1;
consume();
}
},
digitParser = function () {
if (!isDigit(lookahead())) return false;
output *= 10;
output += (consume().charCodeAt(0) - 48);
return true;
},
numberParser = function () {
signParser();
while (digitParser());
};
input = s;
numberParser();
if (!input.length === 0) return false;
output *= sign;
return output;
}
As is always the case with this kind of interview questions, nobody would seriously expect the interviewee to just write those functions down on the whiteboard. Especially the recursive-descent parser. But IMHO, anybody should be able to sketch out what the function would look like. In particular, one of the beauties of a recursive-descent parser is that it is a very direct transformation of a context-free grammar into a set of parsing functions, and an interviewee should be able to explain roughly how that transformation works, and what kind of parsing functions correspond to what kind of grammar constructs.
phew, that is a lot of stuff that you can get out of such a simple question!