Showing posts with label Sudoku Solver. Show all posts
Showing posts with label Sudoku Solver. Show all posts

Wednesday, May 2, 2018

Why I work on mock interview and choose to be an interviewer?

May 2, 2018

Introduction


I had a mock interview this 10:00 PM. At the end of interview, the peer asked me why I choose to be an interviewer on mock interview. So I like to write a small research on this topic.

Will continue to work on it.

Sudoku solver algorithm 


Learning one algorthm very well. It takes determination to learn one algorithm over years since I could not believe that I am too busy and I have to constantly remind me to work on the same algorithm and get better and better. This will make my life as a software programmer much easy.

It is very important for me to learn the algorithm very well in order to sustain my career as a software programmer. I like the mock interview since I love learning and teaching, and also I do see the opportunities to improve through each mock interview.

I like to write down my experience of learning Suodku solver algorithm. I started to work on the algorithm and documented my practice in a few of blogs in 2015. I thought that I did very well to master the algorithm.

I got to know the truth once I start to practice mock interviews.  To work on the sudoku solver algorithm in mock interview, one time a senior developer who works for fortune 500 gave me a rating of 1 out of 4, and later in mock interview the peer told me that I should work on the structure of depth first search algorithm. I have worked on 10 rounds of mock interview, so I have chance to work on the mock interview over 20 times. I also asked the question on code review website.

It is very important for me to learn the algorithm very well in order to sustain my career as a software programmer. I like the mock interview since I love learning and teaching, and also I do see the opportunities to improve through each mock interview.

Learning and teaching 



I remember that I used to read a post by a high school teacher in early 2006 when I worked on computer science Ph.D. in Florida, who is my second elder sister Jianhua's friend. My sister Jianhua has over 30 years teaching experience. Basically the friend wrote in her post is that learning and teaching is kind of repetition. You got so many feedbacks coming in, bad or good. You have to work on those feedback, integrate them to next teaching or learning.

Learning algorithm is kind of the same thing. You practice the algorithm through mock interview, work with different people who is also hard working. And then you will get a lot of things to work on.

Do not stop. Keep continue. Work on the same algorithm again and again. You will find things to work on besides memorizing the solution. There are a lot of challenging issues.

My mental toughness


I had a mock interview with a friend on May 2, 2018. After more than one hour mock interview, the peer gave me honest feedback. I should memorize the common solution for those algorithms. If the competitor makes it in five minutes, I take 20 minutes, I will be in trouble. I will not have chance to get the second question.

Also here is the quote:"Must do interview questions
Each topic has some building block questions which end up being used by a lot of solutions. It is important that you already know solutions to all these problems so you don't waste time thinking up the solution to these problems and focus on the question interviewer has given you because you will be judged on that. I have included some of  these questions but if you go through Interview Bit in detail you would eventually come across/cover most of these questions"


I gave my thoughts as well. Here is my argument:

I always watch those tennis professional player up and down, so that I will not get disappointed too long if I fail. I used to hit rackets to the net but opponent complained to me. I like to practice better than real interview.


I like to practice than real interview



I like to write a few sentences for this argument. I do think that the more I meet peers through mock interview, I know better to do current job. My achievement is tremendous and make me grounded. I am not trying to achieve unachieveable target, serve others if need, teach or learn.

Share the experience


I like to document my own practice. It looks like that I am the luckiest one to be able to document my algorithm practice more than a few years. I treat the code like commodity. I can produce and consume any time. I do not need to worry too much how good my code is. If it is not, I will get better next blog or next practice. If it is good, then it will be such a fascinating marketing tool for me to get connected to more people.

Love to code every day


It is like sports activity. To maintain the physical health and mental health, I always play sport and also matches to encourage myself to get in teams, and continuously learn new things.

I got feedback from binary tree level order traversal from my mock interview performance on May 2, 2018 9:40 PM. I use extra node null to mark the end of level, but there is more popular trick to use a nested loop for the current level, and leave the outside loop for the level update. I missed that part, and the peer told me the truth, honest feedback. He asked if I like honest feedback or nice sayings. I chose the honest feedback.

I practiced the algorithm over 2 years ago. I just reviewed my past practice, and also spent hours to go over Leetcode discussion first time on this algorithm called binary tree level order traversal.

Here is my C# code written for binary tree level order traversal.

Here is my C# code written for zigzag level traversal.

Tuesday, April 24, 2018

Being interviewer: Sudoku solver

April 24, 2018

Introduction


It is so interesting to meet the peer second time in less than a week. I had good time to learn Go language through the peer's performance. One major advice I had after the peer ran the web compiler test cases is to add back tracking on line 35, so the code could pass all test cases. I explained to the peer that parent node will ask child node to back track the element to its original dot value since parent node will try its next option.

Mock interview


This mock interview is so much fun, since last time I was complained not to give out any hint, leave first 30 minutes for the peer to reach his full potential. The peer gave the time complexity analysis and mentioned that he learned the recursive tree through cracking code interview book.

Here is Go code I reviewed, which also passes all test cases.


Saturday, April 21, 2018

Being an interviewer: Sudoku solver

April 21, 2018

Introduction


It is challenge for me to train myself to be an excellent interviewer. I noticed that I checked wechat and read my own blog while the peer worked on C++ code. The first 30 minutes the peer could not pass all test cases, I told her that I am not in the rush, let me review her code and find the bug together.

The problem is that I should give the peer hint every step she writes the code, and point out the bug in her process. But the peer was trained and advanced to ICPC national contest in Brazil, and she wrote the idea kind of new to me. I let myself skip the whole process, and then catch up last minute how the code works. Specially she got some help from a friend to play world contest of ICPC.

Mock interview


Here is C++ code. I learned a few things about C++ through mock interview. The peer told me that in C++ 1 means false.

I ended up talking to the peer one hour 40 minutes. She is preparing Microsoft onsite interview in May. I definitely learn a few things through her practice. I understand that ICPC contest is really good tool to help students to advance their problem solving skills in quick and efficient ways, specially before they start to work full time as a software programmer.

Highlights of code review:
1. line 6, k argument is not meaningful, it should say something like digit.
2. line 14, the peer mixed col/3 with row/3.
3. line 61, the peer told me the failed test has two 9 in the same column, so the call of fill should be checked and return false if the duplicate is found.


Being an interviewer: Sudoku solver

April 21, 2018

Introduction


It is my 10:00 AM mock interview. The peer worked on Sudoku solver, and then he chose swift language. What he did is to check given numbers on the board are following the rules - row/ column, small grid 3 x 3 matrix not duplicate.

The code he wrote for preprocessing is kind of buggy, so we started to talk and communicate on this piece of code. And then I told him that most of people do not do this preprocessing.

The interviewee wrote code but could not pass the test case, and then I walked through the code, and pointed out on line 18, variable name i is used the second time.

Code review 


Here is the swift code I reviewed for the peer as well. The code passed all test cases.

I gave the advice to write readable code, use meaningful variable names, and also I advised the peer to communicate more with the interviewer. I told him that I met a young master graduate, who was preparing Google onsite, and then he got offer. When I mocked interview him, he kept asking me "am I in right check", "do you think that my coding looks ok", he has determined goal, to write code to pass all test cases he prepared, he likes to develop a complete solution in less than 30 minutes.

I had a small chat with the peer who is preparing Uber phone screen. The peer got the Amazon offer but chose another offer a few years ago. Interesting story, a small world, stay anonymous, smart and hard working young programmer.



Monday, April 2, 2018

Sudoku solver

April 2, 2018

Introduction


It is such great experience to work on Sudoku solver in mock interview. I wrote the algorithm in less than 30 minutes.

Code review


Here is my analysis with C# code.


Sunday, March 25, 2018

Being interviewer: Sudoku solver

March 25, 2018

Introduction


It is such great experience to work with a software engineer from Israel 10:00 am mock interview.  The peer also told me that she works for one of top four software company in the world. She worked on Sudoku solver.

As an interviewer, I like to write down some highlights for my code review.

Code review


Here is the solution I reviewed. First of all, the peer wrote down the analysis very clearly and asked me if I followed her idea after she wrote some of the code from line 21 to 44. After she wrote the solution to call recursive for next iteration, she asked me the feedback.

Highlights of my code review:

1. line 25 to line 26, I added those two lines. I explained that there is a bug to find next position of '.' char. Line 23, I changed column = j to column = 0, and add line 25 and line 26 to start from (i, j).

2. line 56, I explained to the peer that no need to check, HashSet.Remove can handle the case if the char is not in hashset.

3. line 88 to 91, I explained to the peer why backtracking is necessary. Her argument is that next iteration the element will be replaced by the available number. And then I gave her the example, first row with first two elements 5 and 3, and 3 empty space with '.', if we work on the third '.' and try the last available number but fails, we need to put '.' back and then continue to work on second '.'. Recover the original status.

Using graph like the following:
5 3 _  _   _
              _ fail ->'.', work on third dot, (0, 4)
          next iteration to work on second '.'

4. work on nextRow and nextCol calculation. Originally the peer wrote (emptyRow + 1)% n, (emptyColumn + 1) %m,


Actionable Items


It is very good to work with the peer on this algorithm. Today is Sunday. I choose to stay at home and work on mock interview. Yesterday I spent over 90 minutes to work with a peer on budget cap calculation. Later I talked to my roommate Emma, she told me that I have to think for other people. If I meet  a strong player on algorithm and data structure, I will talk over 90 minutes. Think about the other way, same applies to the peer I met as well.

Emma is a very good IELTS teacher, and she told me to work on cleaning the living room table together. What I did is to learn and stay organized. We both laughed about the shopping I did in Seattle, and I did make a mistake to purchase too many Nike shoes for myself. I bought 3 pairs of Nike shoes, one pair I wore two days, it broke my skin near my ankle. I went to Seattle with my friend from Shanghai, she laughed about me how I can wear 3 pairs of sneakers. It turned out not cheap for 3 pairs of shoes, $150 Canadian dollars.

Sunday, January 7, 2018

Sudoku solver

January 7, 2018

Introduction


It is another mock interview, Sunday 10:00 AM. I had the chance to work on sudoku solver, and then I spend 30 minutes exactly to solve the problem, first few minutes to explain the problem, my solution, and then I wrote the code and pass all test cases.


Code review


Here is my C# code.


Saturday, December 9, 2017

Education of computer science

Dec. 9, 2017

Introduction


It is normal 10:00 PM mock interview, but this is the first time I met a Chinese and also there was issue about audio through mock platform. I suggested to use skype phone number to get audio, instead the peer suggested to use Wechat to get audio. There are two algorithms involved, one is spiral matrix print for me, one is Sudoku solver for the peer. We talked about the algorithm and reviewed the code for one hour, and then we spent time to share the experience through wechat until 12:40 AM. It lasted 2 hours 40 minutes.

Backtracking bug


The peer wrote a Sudoku solve with a back tracking bug. I told the peer that Let me review the code and also help to find the bug.

I gave advice on coding style. Do not use i, j instead of using row and column.
Suggest to calculate the next row/ next column and express the intent to calculate.
Write first line of code in the function to discuss base case.
Remove if/ else and nested if/else, make the code flat, avoid smell of arrow-like code.
Most important is to discuss the backtracking and why it is so important to do it.

I enjoyed the discussion, I did see that people do not do backtracking and code still passes all test cases before. This time the backtracking has to be done, otherwise 5 out 7 test cases failed.

For example, Sudoku first row:
5 3 _ _ 7 _  _ _ _

For example, first _ can be filled with 9 possibility, from '1' to '9', for example, for example, '1' is tried, and then continue to second _ which is row 0 and column 3, '2' is put into trial. If '2' fails, then second _ with index of (0,3) should be backtracked to be filled '.'. We need to go back to the first _ to try next available number.

I showed the peer things to work on. Also I learned from the peer through his experience, he had 8 phone interviews for intern, 2 rounds of phone interview for interview, he definitely almost made it. Apple, Facebook, Uber and Microsoft.

All advice are from the code review on stackexchange.com, the question I posted over one month ago.

Talk of graduate study 


First document some facts about computer science graduate study and questions and answers:

1. It is hard to get in the computer science master degree program in Northwestern university. The tuition is expensive compared to Santa Clara university, $18,000/term vs $9,000/term.

2. To get a computer master degree, there are 45 credits in the program, non-thesis program. The tuition is around 45 thousand dollars, each credit costs $1,000 dollars. Private university, same tuition for international or noninternational.

3. Around 100 computer science master degree student, half are Chinese, half are Indian.

4. What is advantage of studying in Sillicon Valley?

5. How many of them get intern at Facebook or Apple? Compare to Waterloo university in Canada undergraduate program?

6. What is policy of work visa in USA? OPT or summer intern, 60 days limitation.

7. What is the like to work full time? Do you experience the tough algorithm similar to Leetcode algorithm?

8. What is difference between students with Leetcode practice and full-time programmers with same practice?

9. Rent fee in California, $800/ month for one room or $1100/month to share 2 bed/ two bath condo.

10. Teaching and learning in the university, what is difference? Santa Clara university vs Northwestern university.

11. What is my motivation to practice mock interview?



Tuesday, December 5, 2017

Leetcode 37: Sudoku Solver

Dec. 5, 2017

Introduction


It is the classical depth first algorithm called Sudoku solver. I had great time to practice mock interview and completed the code and passed all test cases in 28 minutes. The peer was very helpful, and I was told that there is a bug on line 29 and missing a function argument on line 58 after I did whiteboard testing.

Here is C# code.


Dec. 6, 2017

Two more comments after mock interview, one is to understand C# keyword const and static, and second one is to think carefully about const variable meaningful name.

Static vs Const


I wrote line 5 public static const int SIZE = 8; in mock interview, compile error, so I quickly removed const keyword in the mock interview. Need to look into the const in C# again.

Later I learned that const is static automatically, but static can be modified, not readonly. So I should write line 5 like the following:

public static readonly int SIZE = 8; 

or

public const int SIZE = 8; 


SIZE or LASTCOLUMN


It is good to name a variable using meaningful one. Let us discuss more here.

Board is 9 * 9 matrix, I added = sign after < on line 9 to make line 9 in the following:
board.GetLength(0) <= SIZE

better saying
board.GetLength(0) <= LASTCOLUMN


When I did whiteboard testing, I added equal = sign.The matrix's SIZE is 9, and LASTCOLUMN = 8. So the variable name on line 5 should
better be called LASTCOLUMN = 8

Line 5: public static int LASTCOLUMN = 8.

Actionable Items


1. Read C# Keywords/Modifiers/Access Modifiers/const, the link is here. There are around 12 access modifiers, plan to read one by one. Abstract, async, const, event, extern, in, out, override, readonly, sealed, static, unsafe, virtual, volatile.

2. C# static vs const on stackoverflow, link is here.

Plan to read 10 minutes on this discussion:

Argument or facts:


It is interesting to note that const members are always static, whereas a readonly member can be either static or not, just like a regular field.

3. One more thing is to review code review website the algorithm I asked about Sudoku solver, and understand the code review about complaints about const value 8 or 9 all over the code. 

The code review is called Sudoku solver recursive solution with clear structure, I posted it 30 days ago

Friday, November 3, 2017

Leetcode 37: Sudoku solver

Nov. 2, 2017

Introduction


It is the time again to write a Sudoku solver. Here is my C# practice, the peer told me that I do not need to run the code using web browser compiler.

The mock interview was supposed to end in one hour to 11:00 PM, but it actually ended at 11:50 PM. I spent more time to chat with the peer until the session terminated due to timeout.

After that, I started to run the code, first I fixed the grammar error line 62. The Split function of string will have an array of string, not an array of char. The line 62 has compile error. It is the easy and quick fix. But I came cross the index out of range run time error. This one I could not pinpoint the error.

I was troubled and tried to set up test case until 1:00 AM, and then I called it a day. I did not do whiteboard testing, instead I tried to guess what is possible places to go wrong for index-out-of range error. With whiteboard testing, it should be a quick fix. But without it, I wasted almost one hour but I still could not find the issue until 1:00 AM.

Whiteboard testing


The whiteboard testing is to use a test case and go over the code line by line, write down the value for each variable, and run the code virtually with results.

If I do whiteboard testing, I should have found the bug on line 91. Here is line 91 with the bug:

for(int row = startRow; row < row + 3; row++)

actually when I run the whiteboard testing, startRow = 0, then row is from 0 to 2. The statement should be written in the following:

for(int row = startRow; row < startRow + 3; row++)


Nothing beats whiteboard testing 


In terms of bug fixing, nothing beats whiteboard testing. Go over a simple test case, go over each line of code, put the result next to the code.

Actionable Item


C# code is updated and it passes all test cases after mock interview. The C# code is here

Review sudoku algorithm on code review website, and I studied the question:

Solving Sudoku using backtracking

Plan to write C# code based on the above code review. I feel that there are a few things I can apply to my mock practice code. 


Plan to write a blog to review algorithm of longest common subsequence written in Chinese. The link is here.

Saturday, April 15, 2017

What do you love most about mocking experience?

April 15, 2017

Introduction


Julia worked on mocking experience starting from April 1, and so far she had 8 experience. What did she like most?

She just checked this video about tennis, what do you like most about tennis. The video is here.

Tennis is last real gladiator sports left.
Even gentle guy likes to crushing experience ...?
Love competition
Atmosphere is just great!
Sound of hitting balls on sweet spot.
What do you like most?

So Julia likes to relate to algorithm and data structure mocking experience, and what she loves most about the experience.

Learn from most talented peers how to perform the problem solving. 



Small talk 


Real fun to go up-and-downs with score, rank from 1 to 7. 
Good experience to journal and then make it better next time. 
Met people and had big surprise how talent people are. 

Let us read some report and get it serious. Understand the performance like sports, win or lose not controllable, understand what is bias and how to fight against odds to show some real work.


Work with people is most difficult part as a software programmer. Julia likes to learn how to work with people and then she can focus on more on technical skills.

Learn to play better mocking experience. One a time.

Facts to remember


April 11, 2017, Know something, Score 1 - "Do not write code until you have a clear algorithm in your head"

In other words, Julia, it is a good idea to write pseudo code first before you write real C# code. Then you can review your algorithm design, clear out any mistakes in thinking process.

Forgot to write base case for DFS algorithm - recursive function in her first writing and then she quickly added it after the reminder. The algorithm is called sudoku solver.

Tuesday, April 11, 2017

Sudoku solver talk

April 11, 2017

Introduction


Sudoku solver is such interesting problem and Julia wrote more than 3 solution in her practice back in 2015. Here is one of the blogs, and also Leetcode 37: sudoku solver is also very good material to review the algorithm.

Sudoku solver talk 


Here is the C# code written in 30 minutes mocking experience.

Transcript is compiled to make it work. C# code is here.

Highlights of a few corrections:

3 nested for loops. The first 2 for loops is to find the first cell with value -1 to do search, try numbers from 1 to 9 and start a DFS search, if all of them are failed, then return false; otherwise return true.

Need to talk about optimization, use the first one with value -1 to start DFS or other cell?


Follow up

Sept. 6, 2017

It is very important to learn the algorithm. Start from code review and learn from the important message in the review.




Sunday, July 19, 2015

Leetcode 37: Sudoku Solver

July 19, 2015

Problem statement:
Write a program to solve a Sudoku puzzle by filling the empty cells.
Empty cells are indicated by the character '.'.
You may assume that there will be only one unique solution.

Solution 1: 

Great blog to read:

http://blog.csdn.net/fightforyourdream/article/details/16916985

And then, start to implement the solution using C# code:

https://github.com/jianminchen/sudokuSolver/blob/master/Program.cs

Solution 2: 

Read the blog,
http://blog.csdn.net/linhuanmars/article/details/20748761

and then, implement the solution using c# code:

https://github.com/jianminchen/sudokuSolver/blob/master/Program2.cs

Solution 3: (good workout on C# KeyValuePair class)

and then, convert C++ code to C# code from the blog:
https://github.com/yinlinglin/LeetCode/blob/master/SudokuSolver.h

Excellent code in C++, using class for node on the board. Learn a few things, fun to play with the code

C# code:
https://github.com/jianminchen/sudokuSolver/blob/master/Program3.cs

solution 4:
https://github.com/jianminchen/sudokuSolver/blob/master/Program4.cs
source code from the blog:
https://github.com/xiaoxq/leetcode-cpp/blob/master/src/SudokuSolver.cpp

Solution 5:
read the blog: (Good coding! practice more based on this blog)
https://github.com/zwxxx/LeetCode/blob/master/Sudoku_Solver.cpp

and convert the C++ code to C# code, (great workout on C# LinkedList for blank nodes)

https://github.com/jianminchen/sudokuSolver/blob/master/Program5.cs

Solution 6:
read the blog:
http://shanjiaxin.blogspot.ca/2014/04/sudoku-solver-leetcode.html

and convert Java code to C# code, great workout on C# and logic checking "return false"

https://github.com/jianminchen/sudokuSolver/blob/master/Program6.cs

Solution 7:
blog:
http://www.jiuzhang.com/solutions/sudoku-solver/
C# code:
https://github.com/jianminchen/sudokuSolver/blob/master/Program7.cs

Solution 8:
Thanks for the blog's highlight line of code on back tracking; finally, I got it! My logic thinking has flaws on back tracking; extra backtracking is not a good. Minimize the back tracking, only do it when "return false". It makes sense to do that.

blog:
http://bangbingsyb.blogspot.ca/2014/11/leetcode-valid-sudoku-sudoku-solver.html
C# code:
https://github.com/jianminchen/sudokuSolver/blob/master/Program8.cs

Solution 10:
blog: (Excellent implementation! no extra line or number in the code! Best for memorization! Go through other solutions later. )
https://github.com/rffffffff007/leetcode/blob/master/Sudoku%20Solver.java

C# code:
https://github.com/jianminchen/sudokuSolver/blob/master/Program10.cs

算法理解了, 代码可以记住了; 开始看不同的题解, 看看高手的代码; 从不同的题解中, 模仿模仿! 像打网球, 多接触不同的打法, 开阔眼界; 接着看这道题的题解. 试着从不同角度看一个问题, 多练习改代码; 看自己能不能有自己的看法, 去尝试一点更改, 玩一点花样; 增加练习C#编程的机会.

Also, the code written has been work on readability, learned through my favorite book reading:
http://shop.oreilly.com/product/9780596802301.do

Those favorite rules I like to learn, pick up and follow:
Big fan of DRY (Do not repeat yourself) principle, do one thing a time, break giant expression, using explaining variable or summary variable, and abstract the thing to a function, extract a subproblem to a function. The code is also modified to fit into short memory, less mental baggage to read through. 



Read solutions:

https://github.com/jordandong/myleetcodes/blob/master/SudokuSolver.cpp

https://github.com/Sayericplz/myleetcode/blob/master/isValidSudoku.cpp


BFS, using queue - try to convert it to C#
http://yucoding.blogspot.ca/2013/12/leetcode-question-sudoku-solver.html