Jan 30, 2006

An example of how not to answer an interview question

I have a bunch of technical questions I typically ask candidates. One of which deals with threading and goes like this:

The following code is being executed in a highly threaded environment.
1. Why does the Debug.Assert statement sometimes fail?
2. What can be done to stop this from occurring?

public class Warehouse
{
private int stockCount = 0;
public void DecrementStock ()
{
if ( stockCount > 0 )
stockCount--;
Debug.Assert ( stockCount >= 0 )
}
public void IncrementStock()
{
stockCount ++;
}
}


The answer is a beauty:

Remove the code in decrementing the stock… this would make the condition of Debug.Assert to always true.

What a great answer - if in doubt, delete the code. As long as the Assert validates then the system must be correct. Why, even the unit tests would all work then. Genius!!

5 comments:

  1. Personally, I think this is a simply genius solution to solve this problem. Why are you bagging it?

    ReplyDelete
  2. I think the question itself is a bug, please read your code carefully!

    ReplyDelete
  3. Why would it matter? The class is pointless, there is never any output from it.

    ReplyDelete
  4. And what magnificent unit tests were you planning on running?

    [TestFixture]
    public WarehouseUnitTests
    {
    [Test]
    public void WarehouseTest
    {
    Warehouse warehouse = new Warehouse();
    warehouse.DecrementStock();
    Assert.?!?!??!?!?!
    }
    }


    Hahahhaahahah!

    ReplyDelete
  5. By the way, your blog pages don't work properly on Safari. The comment links go disappearing when you put the mouse pointer over them.

    ReplyDelete