Super Secret Interview Question Answered!

| | Comments (6)

Yesterday you saw the questions that gave me a lot of consternation. Hopefully I'm not alone! ;-) However, there are answers to the questions.

The first two should be relatively easy. The first question is generally answered with an iterative function where you loop until you encounter a null byte, converting as you go. So the quick way to get an integer value from the string is to subtract '0' from the character (since ASCII values are just single-byte values). So '1' - '0' is the same as 49 - 48 (the ASCII values of '1' and '0'), which is 1. Then you can use the pow function to position the value properly (if you have a 3 character long value like "103", then 3 is 3 * 10^1, 0 is 0 * 10^2 and 1 is 1 * 10^3, etc).

The second version is a very standard question as well, just worded differently. Basically, it's asking you to take the iterative function you wrote and turn it into a recursive function. So instead of using a for loop, you simply use recursion. Again, fairly straightforward.

Up until this point, I thought I was doing pretty good. They were easy questions, and pretty standard ones at that. It was the third question that threw me for a loop.

It turns out that the way you accomplish it is with an array of 256 function pointers. Every time you get a character, it's ASCII value is an index into the array of function pointers where the entries for '0' through '9' are calculating the value (with the help of a global position value). The entry for the null byte terminates the process and returns the proper value. All of the other entries point to an error function.

Evil, Mars... very evil. I love it. I plan to remember this question so I can ask it whenever I need to interview people.

6 Comments

#1 pow is not necessary to get the right answer for the first one. Just multiplying the accumulated result by 10 each time through the loop is sufficient.

For #2 did you use head or tail recursion ? I suppose you could use either but head recursion seems better suited.

As for #3 I have to admit I did not think of a table of function pointers

That does make it tricky

I'd argue that recursion is a form of looping. It's iteration with a stack. So I think I would have ruled it out as a legal answer and would have been left with nothing :)

The function pointers answer to #3 is very clever.

Yeah the function pointer mechanism in #3 is clever. But if you are not allowed to use any looping construct and recursion being one form of looping mechanism, my question is how would you traverse the string in the first place? Would you also assume it to be a maximum of 10 digit number and write same piece of code 10 times to traverse??

The key here is that recursion is not considered a looping construct. Remember, this is from a compiler's perspective (REAL Software is a compiler company). Calling the same function is no different than calling a different function to the compiler: they're both a call instruction. Looping constructs are entirely different (involving branch and comparison instructions).

So, if recursion is allowed then this small recursive function should work just fine for the 3rd scenario.

int StringVal (const char *buf)
{
int value = 0;
static int index = 1;
const char *tbuf = buf;

(*tbuf != '\0') && (value = StringVal(++buf), value += (*tbuf - 48) * index, index*= 10);
(*tbuf == '\0') && (value = 0);

return value;
}

You get a ton of points for creativity on that one because your code does work. If you answered that during an interview, I'd certainly give you credit for it. However, doing a disassembly of what that code does, you'll see control statements (test, je and jne x86 instructions). But that's still an excellent answer.

Leave a comment

Disclaimer

I'm currently an employee of REAL Software. My blog is mine. The opinions represented in this blog are mine as well and may not represent my employer's opinions. All original material is copyrighted and property of the author.

REALbasic® is a registered trademark of REAL Software, Inc. REAL SQL Server™ and Lingua™ are pending trademarks of REAL Software, Inc. All rights reserved.