Thursday, September 29, 2011

Funny Bugs Make Good Interview Questions

I'm currently collecting my set of standard interview questions for future candidates.  The following is one of the those which would be in my list, and it is coming from a bug that I found recently.

I was tracing some bugs few weeks ago and found out the following code snippets ( well, it is a modified version ).

Can you spot any potential problems for the following code?


filterResult: function(query, collection) { 
  var matches = [];
  var resLen = collection ? collection.length : 0;       
  for (var i = 0; i < resLen; i++) {            
    var item = collection[i];
    var itemStr = (item.name + com.search.ResultCache.PIECE_DELIM + item.id).toLowerCase();          
    if (itemStr.indexOf(query) != -1) {
 matches.push(item);
    }
  }
  return matches;
}


And my next question would be asking from performance and best practice points of view, how they could enhance this code.

Actually fixing this bug doesn't take that long, but the process of hunting this bug and tracing the logic is tedious.  The function works 99.9% of time, but it failed on a small number of test cases.  And the process of collecting failed cases and understanding the relationship among those failed test cases took quite some patience.  But seriously, it is fun :)

No comments:

Post a Comment