Showing posts with label Practice till you get it right. Show all posts
Showing posts with label Practice till you get it right. Show all posts

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.

Monday, November 7, 2016

HackerRank NCR codesprint - Spiral message - After the contest

Nov. 7, 2016

Problem statement


Previous blog about 15 submissions in the 48 hours contest.


After the contest, Julia downloaded all test cases, input/ output, she played with test cases, after more than 30 minutes, she finally figured out her major issue on the problem solving.

From the first 3 lines of problem statement:
The message originated as a single line of one or more space-separated words, but it was encoded into an  matrix as a clockwise spiral starting in the lower left-hand corner

Julia did not pay attention to the start point, lower left-hand corner. All her works are from up left-hand corner instead by a mistake

So, there are 4 corners:
up left-hand           up right-hand
lower left-hand      lower right-hand

Work on the sample test case: 
3 5
a##ar
a#aa#
xxwsr
The output of spiral message:
xaa##ar#rswx#aa

She did work on the coding in the contest, her output starting from 'a', not 'x'. In other words, sample test case's spiral message - her understanding and output: 
a##ar#rswxxa#aa


So, in the contest, Julia has to train herself to read problem statement more carefully. 

Ideas:
1. Write down all inputs
2. Make a check list
3. Make her own notes, do some research about the algorithm
For example:
1. a spiral message
2. clockwise
3. starting from lower left-hand corner
4. Not from general case - upper left-hand corner
5. ...


She started to work on the algorithm from 11:00am, Saturday, and then, in the evening, from 11:00pm - 1:00am, she came back to work on the algorithm, she read the problem statement again and again, but she did not notice the starting point - lower left-hand corner. 

Actionable Items:

Do not spend more than 1 hour to write code in the contest; go back to read the problem statement again and again; draw your own diagram - anti-clockwise. 

It is easy algorithm. Julia stumbled on easy algorithm so badly. 

C# code after contest - bug-free, clock-wise starting from lower left-hand corner


Year End review:
Review previous work on array manipulation:

1. Previous work on rotate of array

2. array rotation

3. array rotation (II)

4. array rotation (III)