Viridien Interview Question

I was shown 3 code snippets and asked to identify any bugs and fixes; For one I was asked to optimize the code.

Interview Answer

Anonymous

Jul 2, 2010

In the optimization problem, the code is as follows: for (int i=0;i....) { first loop accessing arr } for (int i=0;i....) { second loop accessing arr } The solution is to modify the second for loop to access the array in the reverse order to make use of the cache. Other modification (I was unable to figure this out) was to convert a division operation in a for loop to multiplication operation i.e instead of diving with x multiply with 1/x. (It seems multiplication is faster on most of the machines when compared to division) Other questions were very simple.