Sunday, November 20, 2016

HackerRank - woman codesprint #2 - stone division - in the contest (series 1 of 5)

Nov. 20, 2016

Problem statement

Stone division

C# submission - need to continue to work on the solution
1. score 0, only pass the sample test case


2. second submission with more problems

Julia's idea to solve the problem:
She stopped on her third algorithm, worked so hard, over 5 hours, messing with recursive/ bugs with concatenating the string - her favorite test case: 2 3 4 6 8, n = 12

She tried so hard to work out on this test case:
    2   3  4  6  8   12
2  T   F  T  T  T   T   -  can divided by 2? 
3


8
  She worked on the backward to find all possible routes - for example, 12, 6, 3, or 12, 6, 2 etc. 


Performance review:

It is hard to review the performance. Because Julia worked damn hard on this algorithm, she tried to push so hard and then she almost fell to sleep, she knew that she should cut the route she took; There is a lot of extra work she carelessly brought into the above submission.

Will continue later.

Binary Search Tree - the algorithm supports the idea to solve "Minimum Cost" (series 8 of 10)

Nov. 20, 2016

Problem statement:

https://www.hackerrank.com/contests/womens-codesprint-2/challenges/minimum-loss



Julia spent a lot of time to try to get used to work on contest algorithms on HackerRank. She saw a lot of efforts she put in, but she was just the beginner of competitor programmer, maybe 10% compared to best talent in the world. 

But, nothing can stop her to mentor herself to get through the stage, and become more confident on the competition. She started to know what to work on in the contest/ after the contest, understand most important things to do in order to get better. 

One thing about the algorithm "Minimum Cost" is to handle the time complexity. Binary Search Tree is good enough algorithm to handle the scale of algorithm. 

Look into the detail of description of problem:
next n years - the span of years - 200000, 0.2 million years, 

n >= 2 and n <= 2 * 10^5, so n^2 will be around 4 * 10^10, 
nlogn is best fit, since logn = 5, if 10 is base of log function. 

No matter the solution is written in C++ or Java, most popular languages used in the top performers, they all chose to use binary search tree based on class, Java TreeSet is most popular one, and then, C++ Set which is also based on binary search tree. 

Here is the just thoughts Julia likes to write a blog about the series, to make it to series of 8 of 10. 

So, in order to understand the contest, competition better, as a former teaching assistant of computer science, Julia likes to study the solution of basic binary search tree, implemented in the way to return the minimum difference as a loss. 

A lot of people stopped in the contest after this second algorithm. Julia felt the same way the difficulty. But she just likes to take one algorithm a time. Do not try to push herself too hard. She stopped on her third algorithm, worked so hard, over 5 hours, messing with recursive/ bugs with concatenating the string - her favorite test case: 2 3 4 6 8, n = 12

She tried so hard to work out on this test case:
    2   3  4  6  8   12
2  T   F  T  T  T   T   -  can divided by 2? 
3


8
  She worked on the backward to find all possible routes - for example, 12, 6, 3, or 12, 6, 2 etc. 

Using Binary search - therefore, it is easy to find the minimum price, O(nlogn)
Maintain a binary search tree!

Study this C# solution using Binary search tree:

https://gist.github.com/jianminchen/c910f5d1f37309c70b489e6c75b0678c

Very classical solution using binary search tree, and very clever solution.

Memorize the solution. Warm up the solution sometimes in the future.

Minimum Cost - comparison of simplicity - C# vs C++ (series 7 of 10)

Nov. 20, 2016

One thing Julia is learning in the contest is to write simple code, avoid complicated situation, cut the executable paths/ giant expression/ complicated things to eat time.

One thing Julia likes to do is to compare her C# solution using Dictionary<Int64, string> to this C++ solution, she just found out that her logic thinking can be simpler.

C# solution written by Julia:

https://gist.github.com/jianminchen/af474846d81b96f4e70fc1a5866dca15

C++ solution: (rank after 400 before 500)

https://gist.github.com/jianminchen/0e98727e40937c11594b0be8ea496885

Compared to top performer (rank 6 out of 1500) - C++ code, just less than 30 lines of code:

https://gist.github.com/jianminchen/8a448bac7de8ad3667b125abe3544e5d

Very close to my idea in C# - by a Microsoft Bing employee -

Nickolas - Topcoder writer, rank 84 of contest.

https://gist.github.com/jianminchen/7ef0dcb49662f277fda36742da5fa57c

Study all her submission on this contest ! - figure out the style I should learn.

Julia's comment: 

Know the problem inside out, a lot of ideas and implementation. One day, Julia can choose best one to make it less than 10 minutes a medium algorithm as well. 

Visual facts to encourage Julia work hard, and continuously improve! The goal is to shorten the time to performance. It is also practical goal, achievable!

Here is the graph Julia likes to share the research she did through the contest:

Case 1:
algorithm: minimum cost
100 minutes vs 8 minutes - over 10 times more to spend on the performance! Julia, you have a big room to improve!

Case 2:
algorithm: stone division, revisited
329 minutes to come out a solution, score 0 of 50; extra 3 hour to work until 2:00am

Compared to 50 minutes full performance, less than 40 minutes.
500 minutes -> 40 minutes, 10 times more! I just could not believe that. The research and development I did over 6 hours, does not help. Best performer uses 40 minutes to do everything, including his research, coding and testing etc.





Minimum Cost - study failed code - partial score 17/ 35 (series 6 of 10)

Nov. 20, 2016

In order to improve the performance on HackerRan contest, Julia tries to look into things she can work on, help her to gain some confidence, relax more when she has a contest to compete.

She score full score 35 on the Minimum cost - HackerRank woman codesprint #2, but she likes to go to extra mile to find out things:

One of failed code with test cases, try to figure out the issue, do some analysis of code.

https://gist.github.com/jianminchen/7401c0b6108659df7d03abd2576cb1cf


Questions:

It is run-time error, why it is not timeout or related to issue. Look into later.

Actionable Items:

Do more research on this, and then figure out how HackerRank will report the runtime error/ timeout. At least know the difference.




C# sortedDictionary - Minimum Cost Algorithm - an idea to write code (series 5 of 10)

Nov. 20, 2016

1. Introduction:
Julia spent time to write a C# solution in HackerRank - woman codesprint #2, using Dictionary<Int64, int> in the contest, here are the detail:

Here is the problem statement:
https://www.hackerrank.com/contests/womens-codesprint-2/challenges/minimum-loss

Julia's C# solution:

https://gist.github.com/jianminchen/af474846d81b96f4e70fc1a5866dca15

The ideas used in the algorithm:

Sort the array with house price.

Use bucket sort similar idea to go through each bucket, compare to previous if the current is less than minimum loss or not. Each bucket keeps the two value - max/ min value.

2. C# solution - Go deep to search for ideas to improve and strength the skill:

1. There is a solution written in C#, much simpler and smarter than Julia's C# code:

https://gist.github.com/jianminchen/c382bc1b40e47c2740a25abcaeffb846

Based on the assumption that housing price is different for each year, but I did not find the word in the problem statement.

2. Using Binary search - therefore, it is easy to find the minimum price, O(nlogn)
Maintain a binary search tree!

Study this C# solution using Binary search tree:

https://gist.github.com/jianminchen/c910f5d1f37309c70b489e6c75b0678c

3. Performance comparison among Java TreeSet, C# SortedSet, SortedDictionary:

Now, she likes to write a C# solution similar to C++14 using Set, Java 8 using TreeSet. Now she will write using SortedDictionary.

C# solution using SortedDictionary, timeout, only score 17.5 of 20.
https://gist.github.com/jianminchen/3e978465798afbd7d611e90a8ad7af0c

using C# SortedSet, timeout, score 17.5 of 20.
https://gist.github.com/jianminchen/63c1ed68999f78a72250372eb58a6953

Look up on stackoverflow.com
http://stackoverflow.com/questions/14675108/sortedset-sortedlist-with-better-linq-performance

Java TreeSet code: Perfect solution, score 20
https://gist.github.com/jianminchen/3fce12eff5838fa10bff0792547d0779

Test code here:
https://www.hackerrank.com/contests/womens-codesprint-2/challenges/minimum-loss

Use LINQ only, no SortedSet, timeout
https://gist.github.com/jianminchen/83f0079acbcfc4b6de3f5b1aff6aa131

Dec. 1, 2016
StackExchange.com Code Review
Julia came out the idea to get help from top talent in the world, she chose stackexchange.com code review and posted a code review request, a few people gave out their contribution, one on LINQ, one on SortedSet, then, Julia learned the solution. The question was posted on Nov. 30, 2016, and then, it was solved on Dec. 2, 2016. Less than 3 days.

Using List<T> BinarySearch
Using List<Int64>, BinarySearch and Insert APIs. Score 24 ( maximum score 35).

https://gist.github.com/jianminchen/d6c675533578d50049c636e566695830

The stackexchange.com code review link:
http://codereview.stackexchange.com/a/148714/123986

Using SortedSet<T> GetViewBetween(), score 30 (maximum score: 30)
http://codereview.stackexchange.com/a/148727/123986

C# submission:
https://gist.github.com/jianminchen/2fda6d1d11b19d6b59f3d44822115927

C++ 14 Set class study - minimum loss - an algorithm - on the hackerRank (series 4 of 10)

Nov. 20, 2016

Julia likes to write a separate blog on C++ 14 Set class. She spent time to read C++ solution on algorithm minimum loss. She like to do some research on C++ Set

C++ Set underneath is a binary search tree.

Here is the problem statement:


Study code:
https://gist.github.com/jianminchen/bae3b7c5c3326213d1ff800aef47ebc8

https://gist.github.com/jianminchen/9098f545f0b750fe1cb4acd6a2c0b1b9


C++ Set analog in C#:
http://stackoverflow.com/questions/575406/what-is-the-c-sharp-equivalent-of-the-stl-set

SortedDictionary<T, U>

Actionable Item:

Study C++ 14 Set class, spend 30 minutes to go over all API - memorize all the APIs.

http://www.cplusplus.com/reference/set/set/set/

26 APIs

begin
cbegin
cend
clear
count
crbegin
crend
emplace
emplace_hint
empty

end
equal_range
erase
find
get_allocator
insert
key_comp
lower_bound
max_size
operator=

rbegin
rend
rize
swap
upper_bound
value_comp


Java TreeSet study - Minimum Loss - an algorithm in contest on Hackerrank (series 3 of 10)

Nov. 20, 2016

Julia likes to put a separate blog to work on the research on Java 8 / TreeSet class - underneath a binary search tree.

She was amazed that the code is so clean and easy to understand.

Study more Java submission with perfect score, best performance compared to Julia's performance.

https://gist.github.com/jianminchen/3fce12eff5838fa10bff0792547d0779


Google search keyword:

Java TreeSet C# analog

Stackoverflow:


Will get into more detail.


Woman's CodeSprint 2 - Minimum Loss - after the contest (series 2 of 10)

Nov. 20, 2016


Julia worked on the algorithm in the contest, and then solved the algorithm with full score - 35.

Here is the problem statement:

Julia's C# solution:


The ideas used in the algorithm:

Use bucket sort similar idea to go through each bucket, compare to previous if the current is less than minimum loss 
or not. Each bucket keeps the two value - max/ min value.

Study all other submissions using C++, Java, C#, JavaScript:

Actionable Items:

1. Read the code line by line, word by word; train myself to understand the code, by reading, by association 
with C#.

2. Study TreeSet - Java - class - memorize all the API, compared to C# Hashset
https://docs.oracle.com/javase/7/docs/api/java/util/TreeSet.html

My favorite Java code:
https://gist.github.com/jianminchen/3fce12eff5838fa10bff0792547d0779

A small research - TreeSet in Java is implemented as Binary search Tree?
http://stackoverflow.com/questions/4430809/making-binary-search-tree

Find the best solution written in Java:

Discussion of Time Complexity:

1. Brute force solution - O(n^2), choose any two year to compare the price. Will time-out!

2. Using Binary search - therefore, it is easy to find the minimum price, O(nlogn)
Maintain a binary search tree!

Study this C# solution using Binary search tree:

https://gist.github.com/jianminchen/c910f5d1f37309c70b489e6c75b0678c

line 16, 25 are Julia's favorite code - 


Very classical solution using binary search tree, and very clever solution.

Memorize the solution. Warm up the solution sometimes in the future.


Woman's CodeSprint 2 - Minimum Loss - in the contest (series 1 of 10)

Nov. 20, 2016

Julia worked on the algorithm in the contest, and then solved the algorithm with full score - 35.

Here is the problem statement:
https://www.hackerrank.com/contests/womens-codesprint-2/challenges/minimum-loss

Julia's C# solution:

https://gist.github.com/jianminchen/af474846d81b96f4e70fc1a5866dca15

The ideas used in the algorithm:

Sort the array with house price.

Use bucket sort similar idea to go through each bucket, compare to previous if the current is less than minimum loss or not. Each bucket keeps the two value - max/ min value.

After the contest study:

There is a solution written in C#, much simpler and smarter than Julia's C# code:

https://gist.github.com/jianminchen/c382bc1b40e47c2740a25abcaeffb846

Based on the assumption that housing price is different for each year, but I did not find the word in the problem statement.

Discussion of Time Complexity:

1. Brute force solution - O(n^2), choose any two year to compare the price. Will time-out!

2. Using Binary search - therefore, it is easy to find the minimum price, O(nlogn)
Maintain a binary search tree!

Study this C# solution using Binary search tree:

https://gist.github.com/jianminchen/c910f5d1f37309c70b489e6c75b0678c

Very classical solution using binary search tree, and very clever solution.

Memorize the solution. Warm up the solution sometimes in the future.

Compared to Java submission using TreeSet class:
https://gist.github.com/jianminchen/6fa69ef28c849d63ed0dc4b419cd0dee


3.


Saturday, November 19, 2016

HackerRank - university codesprint - array construction - code study (series 4 of 5)

Nov. 19, 2016

Plan to spend time to study all 211 submission score 80 -

Array Construction

https://www.hackerrank.com/contests/university-codesprint/challenges/array-construction/leaderboard/3

6 Java/ 8 Java8/ 60 cpp/ 2 C#

Also, study code scored 40 or above 40.

Also, read more code if time is available. Most important is to find ideas solving problems.

Ideas Julia likes most:

ideas about DFS, backtracking, DP, and pruning ideas related to mathematics, algebra. 


1. DP - dynamic programming
See Editorial notes of Array construction
2. ...


Blogs:

https://yourstory.com/2015/11/simran-dokania/

coding blog:  Java

http://problemasdeprogramacao.blogspot.com


Friday, November 18, 2016

HackerRank - university codesprint - array construction - testing and playing after the contest (series 3 of 5)

Nov. 18, 2016

Julia spent another 3+ hours to work on the array construction code study after she understood the algorithm design.  The blog:

http://juliachencoding.blogspot.ca/2016/11/hackerrank-codesprint-array.html

Here is the C# solution she did some research:

1. perfect solution with full score 80
https://gist.github.com/jianminchen/096ebc5bc1769b83b38ec6eeaabbc7c5

And then, she wrote some debug information on the code:

debug code:
C# code with debug info (stage II):

So, she tried to figure out if she can apply the same idea to her code in the contest, but she still had timeout issues. And then, she noticed that line 36 of solution: 
https://gist.github.com/jianminchen/096ebc5bc1769b83b38ec6eeaabbc7c5

--
line 6:
static bool[, ,] w;

line 21:
w = new bool[n, s + 1, k + 1];

line 36:
if (w[p, sum, diffsum]) return -1; else w[p, sum, diffsum] = true;
--

- quick calculation to verify the space limit - 
n<= 50,
s<=200
k<=2000,
so, bool[,,]w size is less than 20 MB.
HackerRank contest space limit is 512MB. It is ok to declare the w[,,] 3 dimension array to avoid timeout!
-- the end of space limit calculation --


She tried to play with line 36, comment out the line 36, run HackerRank test cases, and then, she found out that test case 4 timeout.

Testing and new findings:

1. first, she commented out the code on line 36:
https://gist.github.com/jianminchen/096ebc5bc1769b83b38ec6eeaabbc7c5

and then, HackerRank complained test case 4 timeout.
So, this pruning is important to avoid timeout.

So, she open the test case 4 input:

20
50 200 1860
...

Julia chose first test case by mistake: 50, 20, 1860 instead of 50, 200, 1860, trace file is 193 page long in the word document.

Fact: 50, 200, 1860, out-of-memory, trace file is too big

Julia ran the debug code to get trace:
debug code:
C# code with debug info (stage II):

And then, here is the trace information - 

TraceFile No 1:
https://gist.github.com/jianminchen/2d3622a096c51b0afe81fd60ca9865ef

Open the trace file(TraceFile No 1) using github,

Search the word: w[49,6,282],
Find first, line 4661,
w[p, sum, diffsum] is not true w[49,6,282]

Read content from line 4658 - 4661:

line 4658:
E: Array A is [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,3,3,9]

line 4659:
F: recursive call construct - arguments: (A,6,282,49)

We know that before the small sequence is found, then, w[49,6,282] is called again. We knew that
the first call W[49,6,282] is calculated and did not lead to a solution. So the second time, we
stopped right away.

Search again on w[49,6,282]
See the line 6722:
E: Array A is [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,1,1,4,15]

see the line 6723:
F: recursive call construct - arguments: (A,6,282,49)

see the line 6725:
w[p, sum, diffsum] is true - w[49,6,282] - return -1

In other words, [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,3,3]
(A, 6, 282, 49) is the first time to invoke W[49,6,282], and then, continue to run the code,
did not find the optimal solution.
So the second time, [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,1,1,4]
(A, 6, 282, 49)
w[p, sum, diffsum] is true, return -1.


Julia's notes:

HackerRank advanced algorithm "Array Construction" is really a challenging one. But Julia likes to
have some fun with the algorithm, and then, she just played the algorithm, C# study code, and then,
her own code. Try to find something interesting.





Wednesday, November 16, 2016

HackerRank - university codesprint - kindergarten adventures (after the contest)

Nov. 16, 2016

It is time to learn Segment Tree quickly. Julia worked on Segment Tree a few times in 2016, but when she worked on the algorithm in the contest, she did not come out the idea using segment tree to solve the problem. Segment tree is also called binary index tree.

The best learning experience is a failure. She will remember forever how segment tree is applied to a real story - kindergarten adventures.

Problem statement

Previous blogs about segment tree:

1. Oct. 18, 2016
Segment Tree Tutorial

2. Sept. 25, 2016
Range Minimum Query blog

3. March 4, 2016
HackerRank: Bear and Steady Gene (I)

Study C# submissions:

1. C# code # 1
2. C# code #2
3. C# code #3
4. C# code #4
5. C# code #5

Study JavaScript submission

Study Java submission

There are over 60 solution to score 30 (maximum score 30), go over one by one, and learn a few tips from those solutions. Write down what you like most - 3 things in Java code.

1. Very structured code

2. Java code to study

No. 1
No. 2
No. 3

C solution

Julia's ideas to improve the performance:

1. Try to write some code first, or do some research to categorize the problem, narrow down the algorithm problem - segment tree. (Quickly go over competition books, search ideas!)

2. Either improving the research of categorizing the problem to classical problem, or be practical, using brute force solution to get a few points. (Just do it!)

3. Give up the effort on advanced algorithm, only focus on one of medium algorithms. If Julia work on this medium algorithm in the contest, put 10 hours work on it, then she could score full score. (Aim low target first!)

4. Try to get in Bronze medal first. Work on medium algorithm (maximum score 30) instead of advanced algorithm (maximum score 80). (Play safe! From medium to advanced)

5. Try to work on an algorithm in 2 hours range - Do not spend more than 2 hours on a problem. First work on medium algorithm each one 2 hours first. Increase the chance to score more points.





Learning style - a good tip to share

Nov. 16, 2016

One of daily habit is to look up search keywords through Blogger back office.

Here are the last month search keywords of this blog:
hackerrank bonetrousle
leetcode 146 C#
acm cheat sheet
leetcode 239
topcoder + finding the longest increasing ...
zig-zag traversal of a binary tree in c
camelcase hackerrank solution
pluralsight c++ advanced

Nov. 16, 2016 - one of search from Yahoo.com is about this algorithm:

http://juliachencoding.blogspot.ca/2016/04/hackerrank-connected-cell-in-grid.html


Julia's favorite algorithm - DFS/ BFS algorithm, she did a lot of practice from April - June, 2016. Five months later, it is time to review the algorithm again.

--

So, Julia will refresh the content of those blogs as well, and try to improve the solution/ or blog content as well.

Share the blog statistics of month of November, 2016:




How to prepare for a programming interview?

Nov. 16, 2016

Read the blog, and write down some notes for further research:

http://snip.ly/0aey0?platform=hootsuite#http://www.makeuseof.com/tag/prepare-programming-interview/

Julia's idea - compete the high level, stressful, challenging contest one contest a time, and then try to find the weakness, improve one algorithm a time, find ways to be a better competitive programmer.

Julia likes to challenge herself in sports training last few years. She just got used to do a lot of competitions through the tennis sports, she knew that she had so many failures, uncountable, but also she learned day by day, got very confident on the sports. She worked on tennis sports - on court close to 500 hours last 5 years, watching tennis games/ coaching videos etc. over 200 hours.

So, she tried to apply her tennis sports experience to her personal coding experience as well. Also she is working on her writing skills - blogging is part of her practice, contribution to the IT community, get connected to most talent people in the world.

Nov. 24, 2016
1. https://blogs.msdn.microsoft.com/ericlippert/2004/04/15/writing-code-on-whiteboards-is-hard/

2. http://sellsbrothers.com/tagged/interview

Monday, November 14, 2016

HackerRank codesprint - Array construction - after contest (series 2 of 5)

Nov. 14, 2016

Problem statement:
https://www.hackerrank.com/contests/university-codesprint/challenges/array-construction/submissions/code/7825209


Study C# submissions:
1. perfect solution with full score 80
https://gist.github.com/jianminchen/096ebc5bc1769b83b38ec6eeaabbc7c5

Julia spent more than one hour to read code, but she could not understand the design. So, she decided to work on debugging, add output text info to figure out the design.

Here are workout she did and then figured out the algorithm:

Study more than 2 hours on one of solutions, using recursive solution; but Julia still are not clear about the solution. Need to work on more! Do not give up! Try it every day 10 minutes. It should be easy! 


Julia's work (3+ hours) Try very hard to understand the clever solution by debugging 

-  From the above C# solution:
https://gist.github.com/jianminchen/096ebc5bc1769b83b38ec6eeaabbc7c5

-->   add some debug information to understand the algorithm 

add debugging information to the source code 


-> Here is the log file to understand the algorithm design:

Question 1:
Use your own words to guess how to design the algorithm through debugging process?

Answer:

still confused about line 118, line 119:

118  int newSum     = sum + i * (n - p);

119  int newDiffSum = diffsum + (i * p - sum) * (n - p);

Question 2: What does (n-p) stand for? Can you explain it in one sentence?

Let us work on one more change first:


so, decided to track n-p value on line 118.
C# code with debug info (stage II):


Actionable Items:

1. Run test cases, and compare the time difference 
First, comment out code on line 36, time out on test case 4.

Write a new blog on this testing adventure.
http://juliachencoding.blogspot.ca/2016/11/hackerrank-university-codesprint-array_18.html


2. Study all C# solutions:

2.1. perfect solution with full score 80

2.2. perfect solution with full score 80

2.3. score half score 40

3. understand one term: constructive algorithm
Constructive algorithm: (preprocessing, and then, lookup)
(HackerRank - array construction is a constructive algo.)

Great idea to push hard - cannot get it wrong! that is the attitude for advanced level algorithm involved mathematical analyse.