Showing posts with label linear scan array. Show all posts
Showing posts with label linear scan array. 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.

Saturday, June 3, 2017

Leetcode 605: Can Place Flowers

June 3, 2017

Problem statement is here.

Julia's C# practice is here.

Why it took 48 minutes?


It is a good idea to review the process to get the correct solution, after a few submissions. Through the contest, Julia spent time to play with online judge, she submitted code more than 3 times, failed to pass the test case before submission. She failed twice with two submissions. 

Here are the code with highlights of bugs Julia spent more time she could afford in the contest. To improve her performance, she should aim to complete the easy algorithm in 10 minutes instead of 48 minutes. 



Preparation is the key


It takes a lot of discipline and hard work to be more efficient to write an easy algorithm. Julia likes to set a goal to set 10 minutes for an easy algorithm.

The idea is to go over 4 places she highlighted her mistakes in the first writing. How she introduced the bugs in her analysis.

What are missing to help her to develop the correct code?

She will study a little more and figure out what to make her coding speed up. Learn to use white boarding test, do not back and forth using online judge to correct logic thinking flaws.

Julia likes to change her style in coding. Do not rely on online judge, write a bug-free code using white boarding testing. But she should have done it early and then she starts to build up experience.

It is a good idea to set up a few test cases for the algorithm. Do not rely on the test cases behind the Leetcode online judge.

Spend at least 5 minutes in the contest to set up a few of test cases, and also calculate the answer. Use those test cases to help the design, understand the problem first.

1 0 0 0 0 1 => answer to plant flower is 1 => 1 0 1 0 0 1
0 0 0 0 1    => answer to plant flower is 2 => 1 0 1 0 1
1 0 0 0 0    => answer to plant flower is 2 => 1 0 1 0 1

1 0 0 1 0 0 0 1 => answer to plant flower is 1 => 1 0 0 1 0 1 0 1


Study the discussion



Choose the idea in the discussion - here is the link

C# code is here

Unbelievable clear and short code. It only takes 5 minutes to complete the code.