CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Steve Hebert's Development Blog

Steve's Blog - From .Net to dotMath and everything in between.

Coding Tests in Interviews

I had the opportunity to give a technical interview the other day where I asked the programmer to write a routine that counts the number of bits in a byte. [ Update: I actually asked to count the number of 1 bits in a byte. ]  He could choose the language or even use psuedo-code if he wanted.  The candidate grew nervous and said he couldn't do it without the development environment.  I said it was a single function coding test and I do not expect perfect code, but he simply refused to do it.

I like the approach of seeing how a candidate thinks and operates, and this is the reason for coding a single function.  The candidate expressed a background of working extensively with binary data, so I threw out an example that should have been comfortable for him.  I've been wondering if I approached it improperly, but I really can't see how I could have made this any easier/better - short of “Hello World”.

 



Comments

Steve said:

I didn't view it as a "no-hire" situation, but rather part of a larger look at how the person approaches code writing.
# October 21, 2004 5:36 AM

Darrell said:

I once asked a programmer for another company how he tested his code. The response? Just hit F5 man.
# October 21, 2004 5:47 AM

David Truxall said:

I have done a bunch of interviews lately and have seen more than one person refuse to answer questions that involved thinking through a solution. I wasn't asking for code, just some ideas on how to solve or approach the problem.
# October 21, 2004 5:47 AM

Steve said:

F5 - that's hillarious! Did you burst out laughing?
# October 21, 2004 5:54 AM

Steve said:

David,
Did you take other approaches to explore the thought process? I checked out the candidate's ranking of himself on technology areas to see how his perception met reality. I also asked what the candidate used to design solutions and go through an example.
# October 21, 2004 5:59 AM

Adrian Florea said:

System.BitConverter.ToString(new byte[1]).Replace("-", string.Empty).Length * System.Math.Log(0xF + 1, 2)

:-)
# October 22, 2004 7:25 AM

Adrian Florea said:

Ah, ok. The original text of the problem was: "Write a routine that counts the number of bits in a byte", not like now "the number of '1' bits".
# October 22, 2004 11:42 AM

Steve said:

That's hilarious. I wrote the initial blog entry incorrectly and as I was looking at it I thought to myself "int BitCount() {return 8;}".

I like the function you wrote. I spent a little time seeing if I could tranlsate it to binary (instead of hex) with BitConverter or related function, replace the 0's with empty strings and count the string length. That would be a cool solution that I hadn't considered.
# October 22, 2004 11:55 AM

Steve said:

Sorry for the change, the original problem was too vague.

My original solution was:

private int OnBitsInByte( byte bytValue )
{
int iBitCount = 0;

for( int iPos = 0; iPos < 8; iPos++ )
if((bytValue & (byte)Math.Pow(2, iPos)) != 0 )
iBitCount += 1;

return iBitCount;
}

I've seen people do this with a bit-move in the for loop that's pretty interesting too.
# October 22, 2004 12:05 PM

Adrian Florea said:

"if I could tranlsate it to binary (instead of hex) with BitConverter or related function, replace the 0's with empty strings and count the string length. That would be a cool solution"

It's pretty simple:

System.Convert.ToString(value, 2).Replace("0", string.Empty).Length
# October 24, 2004 12:05 PM

Steve Hebert said:

That's awesome! I love it!
# October 24, 2004 3:12 PM

John Lam said:

Follow-ons to this age-old problem (and generally it's more interesting if you say find the number of 1 bits in a 32 bit integer).

Now optimize for space.
Now optimize for time.
# October 28, 2004 8:46 AM

Cardanine said:

ugh, using expensive Java string operations to count bits? That's what the world is coming to?

int count1Bits(byte b) {
int count = 0;
for(int i=0; i<8; i++) {
if(b & 1) count++;
b >>= 1;
}
return count;
}
# November 17, 2004 11:05 AM

Steve Hebert said:

Yes, it's slow as crap but it's creative and shows awareness of the framework functions. Personally I would prefer a numeric based solution myself.
# November 18, 2004 3:58 AM
Check out Devlicio.us!

Our Sponsors