Showing posts with label spiral message. Show all posts
Showing posts with label spiral message. Show all posts

Wednesday, January 31, 2018

Leetcode 54: Spiral matrix

January 30, 2018

Introduction


It is such a big surprise that my first mock interview as an interviewer turns out big success. The peer solved all four algorithms I asked in first 40 minutes. He is the best peer I have met in my peers, over 150 peers.

Best thing is that I got education how to design a perfect system design: Instagram scalability in 30 minutes. I could not believe that I may interview a top company senior developer in the future.


Code review


Here is the C# code I like to learn. The idea is very clear and it is the production ready code.


Thursday, December 8, 2016

HackerRank - NCR codesprint - Spiral Message - Code Review

Dec. 8, 2016

Problem statement


Introduction


The spiral message algorithm is an easy algorithm, but Julia stumbled on this algorithm badly in the contest, she missed the important part - the message starts from low-left corner, not upper-left corner. And then, base cases should be tested: one node, one row, one column first, and then, four edges are handled from low-left to upper-left to upper-right to lower-right to lower-left.

Here are two blogs showing her work:

1. Blog about performance in the contest.

After the contest:
3. Blog about things to work on after the contest.

Continue to work on the spiral message algorithm.

Workout:

First review the code after the contest.

Julia spent over one hour to do code review, and then, she put together a new version, ready to post on stackexchange.com code review section, ask help:

C# version:


Highlights of change


1. Function name is changed to match the requirement: 

SpiralMessageFromLowerLeftClockWise

2. Test cases are added to spiral message, ensure that the order is correct, not just
how many words in the spiral message.

Important Link to get some feedback from the community on stackexchange.com code review.

Monday, November 7, 2016

HackerRank NCR codesprint review

Nov. 7, 2016

Codesprint:
https://www.hackerrank.com/contests/ncr-codesprint/challenges


Julia was so motivated to spend 2 days in the weekend to work on the codesprint, she only did one shopping trip to Burnaby crystal mall, less than 2 hours. Rest of weekend, she worked on the problem solving.

Julia reviewed the article about HackerRank contest and how to play better:

https://goo.gl/IHeEoi

Her favorite note in the above blog, item 4:

Even if you are not world class competitive programmer, you still have a chance to get into top 50 or even higher. The minimum goal is to get into top100, which is like usually about top 2–3% of all competitors. Being in top100 is great and sounds really good. 

The major factor to achieve the goal is a combination of problem solving skills, online research, dedication and persistence. First read all the problems and start solving them one by one from the easiest one. First solve the ones you can tackle without any research or long thinking, just to mark them as done and get motivation to tackle harder ones.

Julia did some research on this NCR contest, in top 100, even around 100, some of them are ICPC contest winner, score range is around 200 out of 430. 

Julia's score is 62.76, 480 out of 2621. 

Facts:
1. Julia likes to work on those hard algorithms, last 3 of 8 algorithms. She thought that she could make 10 from each of them. But it was too late when she read the problem statement. She only had 3 hour left to 12:00am in the middle night of Sunday. 

Lessons learned: 

Do not work on ideas taking a lot of time to code; even prototype is questionable. Treat it as a contest, play to win, not play to learn

48 hours contest, a lot of algorithms - 8 of them, total time to work on is 16 hours, 8 hours a day. So, give each algorithm 2 hours a time. 

Spent 6-8 hours to score 6 out of 20 on spiral message. Simple mistake of understanding problem statement, and base test case: one line, one row failure. 

Should be more confident on hard algorithm - game of numbers. Less than 1 hour to score 15 of 50, but gave up bug fix. So close to perfect solution. 
So, if Julia has more experience to play HackerRank contest, she should try to make it at least 100 of 430. And also, she can spend less hours to work on the contest, and spend 1 - 2 hours a day on sport activities in the day time. 

Do not think about past contest - Warlmart codesprint, 24 hour contest, Julia spent time to bet on a hard problem (score: 100) until 4:00am, and scored 0 of 100. 





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)

HackerRank - Spiral Message - NCR codesprint

Nov. 7, 2016

Problem statement

Julia spent over 4 hours to work on the solution, she made 15 submissions in the contest.

No. 1: first submission - score 4.49 of 20 (pass 4 of 14 test cases)


submission #2: (Score is 0.82 (full score is 20), pass 2 cases of 14 test cases.)


Julia's analysis after contest about the submission:
Submission #2 issues:

1. Repetition: Code is no good in structure, repetition code.
2. Structure issue: Counting mixes with string construction. Counting code duplicates 4 time. line 96 - 100
3. Base case: base case is not handled properly, one row, one column; one row will be counted twice.
4. Missing info: String split using # is avoided, just use count directly. But the spiral message is not correct.
5. Extra variablesrow, col two variable can be avoided, using startX, startY, endX, endY to calculate.

No. 3 submission

code review after the contest:
  1. The while loop - 115 - 119, but line 118, 119 redundant, part of first two lines.
  2. Base case - one row, one column do not work - count twice
  3. Add debug code - stringBuilder to tracker the string output - it is helpful, but unfortunately in the contest, Julia did not catch the error - starting point.
  4. Missing start time/ end time for submission above function comment, need to track time spent.

line 49 - 59 test function: 

if the testing() function, the program does something like string.CompareTo("xaa##ar#rswx#aa") == 0 (line 209 - 210). and then, Julia would have found the start point should be lower left-hand instead of up left-hand in the contest.


No. 15. Julia's submission - score 6.7 of 20



Editorial Notes:

Think about the problem writing. Good problem writing gives out the important information, directly/ indirectly, more than once, twice.

There are 3 times to catch up spiral message starting point bug.
1. First, by following the diagrams closely.
2. Read the word by word
3. Sample test case spiral message result.

Base test case failed - bug, need to think about Math induction proof, starting from base case, and then, assuming N is correct, prove N+1 case based on N and base cases. Detail see book:
Page 15 - mathematical induction vs  programming technique of recursion

Design talk:
Spiral message - a matrix

start point matters,
choices: start from 4 corners.