Monday, November 28, 2016

HackerRank - Bear and Steady Gene (VI)

Nov. 28, 2016

Julia noticed that those two blogs are most viewed:
http://juliachencoding.blogspot.ca/2016/03/hackerrank-bear-and-steady-gene_5.html

707 views

http://juliachencoding.blogspot.ca/2016/03/hackerrank-bear-and-steady-gene_13.html
699 views

So, Julia decided to review the algorithm, and she learned something new.

Summary of workout:
1. Find the most simple solution to study - two pointers, sliding window technique;
2. Write a C# version - understand the algorithm
3. Code review, and write a new version
4. Submit a code review request - her first coder review on stackexchange.com, and get some feedback.

Details:

1. Study the C++ code:
https://gist.github.com/jianminchen/c6b51207f9cc9b083573
Time complexity of algorithm: O(n)

typical two pointer technique, left/ right pointer, both at most travels forward once. 

Timeout issue - O(n^2) brute force solution 

2. Write a C# version: 
https://gist.github.com/jianminchen/c4f1c84c984e58fdcdc467e6f28d84e3

code review:
1. line 26, int[] a = new int[1007];
1007 is not meaningful, we know the size should be bigger than 'Z', and we only need the array of size 4
2. variable i and index are not meaningful. There are two loops.
3. valid function from line 49 - line 58
   line 52 - 55 - four constant chars are used.


3. Review the code and make the change:
https://gist.github.com/jianminchen/124b33e3d7aa0276e7b6ea4542c8ad5b

1. line 26 - 36, add 4 test cases, with 4 postcondition assertions
2. function name is changed to minChange
3. line 61, array of size 4 is declared instead of 1007
4. function indexOf() is added
5. variable names are changed, left, right, two pointers, move forward only
6. add two explanation variable c1, c2 to advoid complicated expression.
7. valid function is declared using a for loop.

Based on the code review on this post:
http://codereview.stackexchange.com/questions/142808/quick-sort-algorithm/142853#142853
answered by Eric Lippert

4. Submit the code review on stackexchange.com code review, here is the link:

http://codereview.stackexchange.com/questions/148407/hackerrank-bear-and-steady-gene

Review got from the site:
  • In C#, method names should be PascalCased, not camelCased.
  • In minChange, you should use meaningful variable names for acc2
  • In minChange, you can set var ans = int.MaxValue, unless there is a compelling reason avoid using Int32 when you can use int, same with Int16 vs short or Int64 vs long.
  • In indexOf, you can use a const string code = "ACGT"; instead. This gives some compiler optimizations.
  • In valid, (and minChange) you should use meaningful parameter names, n and a don't have any contextual meaning.

Actionable Items: 

Julia likes to do some research, why it takes her over 6 years to start to work on stackoverflow, actively join the community, become one of them. 

Some facts she put together here:

1. Julia noticed on Nov. 28, 2016 that on stackexchange.com code review section, the experienced professional responded to her first post in less than one hour; (a fact)

2. The advice she got is free, she does not need to pay; all she has to do is to share her question and her research; And the comments are very good; She also enjoys the sharing her workout. 

3. Julia tries to get help from other professionals, she was too naive to check statistics of coding blog, but no one could give her any input. Finally, she figured out that she has to get smart, reach out stackexchange.com/ code review, ask help, show what she has completed. 

4. Congratulate Julia to know how to connect with other peers efficiently, her first post on stackoverflow.com - her first 10 points of reputation

5. Julia did some study on stackoverflow - how to ask a good question, and she knows that the question does not belong to her, it belongs to the community. That is the reason she can get help quickly. (her research on stackoverflow over 10 hours in Nov. 2016)

6. Write down her own understanding - research more later: Coding blog vs. stackoverflow contributions

Follow up 



June 6, 2017
Work on Leetcode 187 Repeated DNA Sequence

No comments:

Post a Comment