Interesting interview question #1
January 31st, 2013 by Michael JasperDuring an interview (for a software developer position) with a major employer in the state of Utah, I was asked an interesting programming question:
Create a function that, given an integer parameter, will return the middle three digits
I mentally wiped the sweat away from my forehead: whew — not computing bigO notation, inventing a new search algorithm, or performing my favorite song!
I verbally walked through my thought process:
There will be a need for some looping to trim off the first and last digit of the integer each iteration — perhaps a while loop with the condition that the value is greater than 99? No, there is a better way: I’ll trim the digits recursively.
I asked for some feedback on my thought process, then set out writing some code. I came up with something like this:
1 2 3 4 5 6 7 
function middleThree(value){ if(value > 999){ value = (value + '').substring(1, (value + '').length1); value = middleThree(value) } return value; } 
“Nice solution,” the interviewer replied. “How will your function handle 4501?” Uhoh: It will trim 4 and 1, and return 50. “Hmm… What about 40251?” Darn: It will trim 4 and 5, recursing on 025, which will be interpreted as 25 and fail.
So, as it turns out, programming questions in interviews might not be about creating the elegant, recursive solution — but about asking the right questions. Thorough requirements gathering. Lesson learned.
After the interview, I couldn’t get the question out of my mind, and spent some time giving it a proper solution: handling input less than 999, erroring on even values (finding the middle three of an even digit value is a somewhat undefined behavior), and polishing it off with some unit testing.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 
function middleThree(value){ //Test length requirements if(value < 99){ return 'Error 1: Input less than three digits'; } //Test evenness if((value + '').length % 2 === 0){ //Even input, exit return 'Error 2: Even Value'; } if(value > 999){ value = (value + '').substring(1, (value + '').length1); if(value < 99){ //Return values with leading zeros return value; } value = middleThree(value); } return value; } function log(v){document.write(v);} //Test digits //0123456789 log("1) " + middleThree(1) + "</br>"); //Error 1: Input less than three digits log("2) " + middleThree(12) + "</br>"); //Error 1: Input less than three digits log("3) " + middleThree(123) + "</br>"); //123 log("4) " + middleThree(1234) + "</br>"); //Error 2: Even Value log("5) " + middleThree(12345) + "</br>"); //234 log("6) " + middleThree(123456) + "</br>"); //Error 2: Even Value log("7) " + middleThree(1234567) + "</br>"); //345 log("8) " + middleThree(12345678) + "</br>"); //Error 2: Even Value log("9) " + middleThree(123456789) + "</br>"); //456 //Test ints with returned zeros log("10) " + middleThree(10001) + "</br>"); //000 log("11) " + middleThree(100) + "</br>"); //100 log("12) " + middleThree(10345) + "</br>"); //034 log("13) " + middleThree(10200) + "</br>"); //020 log("14) " + middleThree(5410245) + "</br>"); //102 

http://stuartrutter.com/ Stu Rutter

Adam


Paddy McCarthy