Showing posts with label taste success and hunger for more. Show all posts
Showing posts with label taste success and hunger for more. Show all posts

Monday, July 25, 2016

Short Palindrome - HackerRank - world codesprint #5

July 25, 2016

Problem statement

Julia spent 3+ hours to score 13 out of 40 points, she enjoyed the time to work on the problem.

 A few issues: wrong answer, time out, run time error.

 More than 3 submissions, failed to solve time out issue.

 1. C# solution

 2. Add memorization using Dictionary to save time, failed to solve issues.
C# solution 

 3. add one more memorization, failed to solve issues.
C# solution

 4. Review code and then add some comment, and analyse design issues:

C# solution

Review the design, try to find the flaws - a few ideas to improve timeout, no run-time error issue.

review code and then add comment for function countingPalindromes:
/*
* use two loops
*
* get 0110 pairs
* get 1001 paris
*
* Think about DP solution - dynamic programming - not working, too complicate
*
* brute force solution:
* 0 - start char, end char, and then, count how many possibilities
* Go through O(n*n) case, for any two 0 in pseudo string, check in-between
*
* For example:
* 001010110
*
* The above case, there are 5 '0',
* position of '0' is 0, 1, 3, 5, 8,
* So, any two '0' combinations are 5*4/2*1 = 10
*
* A: 0, 1, in between, there is no chars, n/a
* B: 0, 3, 2 chars in between - "01", count how many '1' in the substring, only 1.
* C: 0, 5, 4 chars in between - "0101", two one inside, so how many combination
* of 11, only 1 choice.
* D: 0, 8, 7 chars in-between - "0101011", four one's in the substring, how many combination - 4*3/2 = 6 choices
*/

Add comment for function getPseudoString(...)
/*
* using 0 and 1 to construct the string
* 1001
* 0110
*
* There are 26 chars in alphabetic string, any two combination is 26*25/2*1 > 250
* Instead, using 0 and 1 to stand for two distinct characters.
* for example, abab
* 0101
* So, we can conclude one thing:
* ababa
* 01010
* or
* 10101
* So, use memorization, 01010 and 10101 should be same key
* See if this can solve time out issues - July 25, 2016
*
* reverse of string should be same key
*/
It took Julia more than 3 hours to gain 13/40 point, but learning experience was so great, and she enjoyed the coding experience.

Starting from brute force solution, she made some progress to gain 13 points; go through optimal solution - DP, should be next step.

Statistics:

450 score 40/40
1600  score above 12/40
Total submission: 2180

Facts about HackerRank:
aiming brute force, 30% score.

Dynamic solution:

detail from editiorial notes.  

The idea of DP from the above website: string length is n

pattern to search

xyyx

xy ends position at i - iterate from 1 to n-1,

denote l[i]

yx starts at position i - iteration from i to n-1

denote r2[i]

yx starts at position >=i

denote r[i] = r2[i] + r2[i+1]+...+r2[n-1]

So, to get a solution:

   Iterate i from 1 to n-1
 
   sum of l[i] * r[i]

And one more thing,
          r[i] = r2[i] + r[i+1]

The idea Julia tried on July 24, 2016 -
suppose that T[i] is the short palindrome like "xyyx" at end of string i, how to construct T[i+1]?

Follow up after 8 months


C# code


Follow up 


8/25/2017
I go through the code, and evaluate the code. The fact is that the code is too complicate for a medium level algorithm. Based on the past code contest experience, the code should be very short and concise.

I will put some code review on my last practice, and then write a solution to help the dynamic programming algorithm practice and learning. Make it part of 10 step series.

Monday, June 27, 2016

A small research on HackerRank world sprint #4

June 27, 2016

 On Sunday June 26, Julia spent 3 hours to do some research, evaluate where she is in the competition, and what kind of target she can set, how to reach the target next time. Actually she went through current leadboard, and study who are best players, top-performance programmers.

Statistics:
Time spent on HackeRank world sprint #4: 4+ hours on June 25, 2016
Score: 40, around 2276 rank in over 5000 people.

Target: 
She needs to score another 50 - 100 in order to be competitive, where she found out that some Google, Facebook employee score' range (above 100, some above 140).

Top 16 - full score (420), there are a lot of world champions over there. So, the problems have some pattern, people can get trained to be good at them.

 Here is the link of HackerRank world sprint #4.

 https://www.hackerrank.com/contests/june-world-codesprint/challenges

 She completed first two easy algorithms in first 2+ hours. Get 40 points.

Gamble the luck is not a good strategy. Set low target is also not a good strategy. 4 hours can be spent on more meaningful learning experience. 

Why? 
Julia spent most of time to work on one problem, AorB, try to score another 50 points. She enjoyed coding and found one problem after another, but it is time-consuming, not efficient approach.

In first hour, she needs to write down every small function she should solve first, and then, design well-defined small function to be helpers, understand possible test cases. Simple problems are easy to work on, save time.

Some problems were discovered after she worked on the problem almost 4+ hours. Julia, you have to learn to be a good thinker first, force yourself think hard on first 30 minutes. Write down things to consider in design of function.

1. What is the problem she found out after 3+ hours?
The solution has problem, Hexadecimal should be reversed first, then, first char is least significant bit, much easy to iterate, compare among A, B, C. Mistakes like A, B, C to compare different bit, not align properly.

2. What is the problem she found out after 4+ hours?
how to make A as small possible? 
Provided with extra allowed bits, Give up 1 from A (1->0), by the order of most significant bit first, and then, set B's bit to 1 if need. 

3. What is the problem she found after 8+ hours? 
A|B = C, so C's length as string should be longest one. C's length = Math.Max(A.Length, B.Length). This helps the design and also testing. 


From 12:00pm - 12:00am, she spent a lot of hours to play with code, until midnight, she gave up AorB, scored 0 with many hours passion/ learning experience/ lesson learned.

Lesson learned: 
Passion does not work, good workout; but not practical. 
Break the problem into 5 - 6 well defined small functions;
First 30 minutes, be a good thinkers. Find all possible problems, assertions, read problem statement again and again. (make sense? :-))

Write a few small functions to warmup, fully test with test cases. 2 - 3 hours.
For example, work on this small well-defined function first,
http://juliachencoding.blogspot.ca/2016/06/hexadecimal-or-calculation.html
https://gist.github.com/jianminchen/5442cb579b13ffc203ce89b3e54e8a36

And then, spend 1 hour to put together for solution AorB.

The best performer in the world (172minutes/7 algorithms), average less than 30 minutes/ algorithm.

Good about the practice:
Practice code on problem solving: 
1. reverse a string in C# -> ToCharArray() -> IEnumerable first
2. check least significant bit using And 1 (bit manipulation)
3. check most significant bit using And 1 - define value array {8,4,2,1}
3B: leftshift >>1 in C# practice
4. Hexadecimal char <-> binary format string <-> integer value expression
5. int, long, bigInteger(?) cannot handle big number 10^5000, force me to work on string manipulation
6. Look into C# const keyword, how to separate variables - using existing variable, set new value, less code.
7. StringBuilder class needs System.Text package
8. break statement - be careful when the function is not well-defined small function, causing bugs.
9. A|B = C, what we can tell the length relationship among A, B, C.

Actionable items:

Choose 10 people's code to read.
Learn to be a good thinker - work on first 30 minutes, thinking, reading, define well-defined small functions.

Blog to read:
https://www.linkedin.com/pulse/why-best-designers-problem-solvers-katy-tsai?trk=hp-feed-article-title-like