Showing posts with label linear scan array look ahead instead of looking backward. Show all posts
Showing posts with label linear scan array look ahead instead of looking backward. Show all posts

Thursday, October 26, 2017

Whiteboard testing is the savior

Oct. 26, 2017

Introduction


It is the best decision to practice mock interview starting from today. As usual, I choose to practice 10:00 PM since it is too late to work and usually I am so tired, usually I have difficult time to think clearly after long day, I like to drill myself to simulate tough situation at work. My last practice is Sept. 27, 2017. Since I took a vacation from Oct. 5 to Oct. 20, I did not practice mock interview at all.

Today I spent first 15 minutes to write code, and then I relied on my techniques using Terse, Express the intent, Do one thing (TED) principle, and then I made the change to look ahead to determine if it is ready to compare (line 33: readyToCompare), I passed all test cases in the 35th minute.

In summary, the algorithm I had to write is a linear scan algorithm, instead of looking ahead to determine if it is the last row in the current time stamp, first 25 minutes I worked on the idea to look backward to compare to previous time. Once I play with whiteboard testing, I figure out the solution 100% correctly.


Code review



C# code is here. Plan to highlight a few changes I made.

Line 29 - comment out line 29, the peer reminded me no need to use startNewTime, previousTimeStamp

Line 40 - max = sum;  // the peer reminded me that sum is the new max value.


Actionable Item


Usually linear scan algorithm I should aim at 10 minutes to perform.