Showing posts with label connected cell in a grid. Show all posts
Showing posts with label connected cell in a grid. Show all posts

Sunday, January 1, 2017

2017 New Year's day - Code Review: Connected Cell in a Grid

January 1, 2017

Ask questions on code review.

Connected Cell in a Grid - Warm up with Five Practices

Spent 2+ hour to put together C# code to post a review request.

Ask ideas how to add some debug code to track quickly/ efficiently the design:

1. Queue - in/ out as designed
2. Connected Region - counting of cells is correct
3. Check how many regions are counted
4. Basically, talking about tips to help development of code, not affect the presentation of code.

Here is the code review feedback:
boundary check and user input handling




Wednesday, May 11, 2016

HackerRank – Connected Cell in a Grid - Warm up with Five Practices (II)

May 11, 2016


Being a software programmer, it is easy to spend hours to read and catch up technologies, work on new algorithm, but no coding, one day, or one week, even half month/ month. 

So, warm up like sports. Julia chose the algorithm - Connected Cell In a Grid to warm up for a few hours. 

Last time, less than 1 month ago, Julia did work on this algorithm – connected cell in a grid. And then, she started to warm up again.

Here is one of blogs last practice on April 16, 2016:

Warmup coding: 
Her practice, using queue, but use jagged array:  (20 minutes to write), using queue, jagged array, 


Question and answer
1. How is the warmup experience? 
Because this is the second one, after 1 hour writing, debugging the code in first practice using dimensional array/ queue, this one is much easy. Just replace the dimension array using jagged array. 

HackerRank – Connected Cell in a Grid - Warm up with Five Practices

May 11, 2016

Being a software programmer, it is easy to spend hours to read and catch up technologies, work on new algorithm, but no coding, one day, or one week, even half month/ month. 

So, warm up like sports. Julia chose the algorithm - Connected Cell In a Grid to warm up for a few hours. 

Last time, less than 1 month ago, Julia did work on this algorithm – connected cell in a grid. And then, she started to warm up again.

Here is one of blogs last practice on April 16, 2016.

First practice, it takes her close to 60 minutes to write, fix issues.  Use dimensional array, use queue to do BFS – breadth first search.


Here are mistakes:
      1. Forget to add boundary check function, do boundary check (source code: line 108)

      2. Forget to introduce neighbor_X, neighbor_Y  (source code: line 90, 91)

      3. Neighbor_X is mistakenly written as neighbor_Y, so wrong answer;
          Debug the code and find the issue. It takes more than 20 minutes, a lot of stress. (source code: line 96)

So, it is excellent chance to learn and improve the performance.

Write a small function to debug the code, figure out the wrong answer issue – testRoutine, source code: line 36.


Second practice, using queue, but use jagged array:  (20 minutes to write)

          

Third practice, use DFS – recursive function, which also returns the count.

         

Fourth practice, using DFS – recursive function, but use an argument – reference int to track value


Fifth practice, using stack instead of recursive function, implement the DFS algorithm:



Question and answer:

      1. What do you learn through the warm up? Do you learn some better ways to fix the bugs?
It is better to write down the functions needed to help the task, this way, you will be more efficient.

Here are 4 tasks:
     1. Calculate the key
     2. Boundary check 
     3. Maximum value search
     4. Using queue to do search

     2.  Why do you do warm up this time? What are the advantages?

Julia still remembers the favorite tip to work on the tasks:
1.    Just mark the visited node as 0 from value 1
2.    Update node value from 1 to 0 before it is added to the queue
3.    Use key = row * 10 + col, since row < 10, col < 10 to track each node in the queue

Julia likes to write code and do some warm up, therefore, she can get more experience; she tries to improve performance to 20 minutes for this kind of DFS, BFS, matrix, search algorithm.

3. Do you reproduce the experience of high stress to trouble shooting and work on bug fix? 

Julia reproduced the issue of high stress, she could not fix the bug on her first practice. So, she wrote a small debug function try to figure out; actually, it is a mistake in writing. 

Next time, reexamine every line of code, every variable, every executable path, when the code is executed. Do not depend on debugging, running the code, because stress level is high. 

Sunday, April 17, 2016

HackerRank: Connected Cells in a grid (IV) - JavaScript code

April 16, 2016

problem statement: Connected cells in a grid



JavaScript submission:
Time spent: 30 minutes

Read 5 - 10 submissions, write down good tips:

All Javascript submissions


1. Favorite JavaScript implementation:

Javascript connected cells in a grid

2. Very structured JavaScript code, using prototype, ===, neighbor nodes - an array - [], define function acting like class Node.

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


HackerRank - Connected Cell In A Grid (II) - C# solution (II) using Queue

April 17, 2:50 - 3:25

Problem statement


First practice using C#,  solution written by Julia:

C# practice by Julia


Study the code: 
1. Use Queue, instead of using recursive calls, using 2 dimension array, excellent code to study:

Study code 

So, Julia did one more practice, and just wrote second implementation using idea in the above blog. 

Write C# code again using idea - 2 dimension array, queue, and also, mark the visited node using '2'. 
C# solution 1

Second Practice

It takes 30 minutes to write and fix bug, log interesting things happening in the practice:

1. First, fix the issue to read a row to a string, and then go over one char a time to get each node for the row.

   use Console.ReadKey()  first <- fatal error

2. use wrong local variable about key

3. Count should be 5 but 11 for one test case, count same thing more than once! - > add extra checking before counting. Or, set node is visited just before adding to the queue. 

Compare to the solution #1, two dimension array uses integer 1 or 0, my copy is using char '1', '0';
And also, the solution #1, every node sets visited true before adding to queue.
And my design, do not set, which causes problem. Same node is added to the queue more than once.

It is better to add unvisited node to the queue once.

Julia, you have to make sure that no bug in the code, it does not matter what idea you use. 

Third practice:

Write in 20 minutes, no bug:

3rd practice using C#


Again, 3 practices:

No. 1 - C# code

No. 2 - C# code

No. 3 - C# code




Saturday, April 16, 2016

HackerRank: Connected Cell in a Grid - C# solution - using DFS

April 16, 2016

Practice one DFS algorithm this Saturday.

problem statement: connected cell in a grid


C# solution written by Julia:


Time spent:  4:48pm - 5:41pm 
20 minutes to think about design, 
30 minutes to write the code
Feeling: nervous when thinking about solution - DFS, it took more than 15 minutes
Study other submissions, and see if I can learn something, also figure out how to cut time to write the code. 

C# solution: 
Time spent: 60 minutes

1. Use Queue, intead of using recurisve calls, using 2 dimension array, excellent code to study.

Tips to learn: 
1. use integer as key: i*10+j
2. mark the visited node using value 2

2. Design the function to return a value 

Julia, people are smart, no need for extra bool array[row][col], just mark the visited node using a new value:

1. mark visited node using 'X'

3. use C# Tuple class, declare a shift array to get more organized. 8 neighbors nodes - go through array twice.


4. study the code


5. declare offset array for x and y - Julia likes the idea.

https://gist.github.com/jianminchen/737138aeb946961aded556181c06502e

Statistics:

1. 10 submissions, there are at least 2 of them using queue - Julia, you have to catch up this using queue!  <- write queue version, practice it 20 minutes, post your version down here!
2. 10 submission, 2-3 DFS algorithm return count <- easy way to track the count
3. Different ways to mark the node is visited.

Reviewed the algorithm on Nov. 16, 2016
- some one found the page through yahoo.com search.