Showing posts with label Leetcode 37: Sudoku Solver. Show all posts
Showing posts with label Leetcode 37: 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.

Wednesday, March 14, 2018

Sudoku solver

March 14, 2018


Introduction


It is my most favorite algorithm in the world. I could not believe that I practiced today with an engineer in Oregon and also I did learn a few things through mock interview. I took extra 30 minutes to discuss with the peer about algorithm practice and exchange ideas. The peer recommended me to practice on codefights.com, consider the purchase of interviewcake.com algorithms with the answers.

I remembered that in 2015 I did study over 5 solution on Sudoku solver algorithm, I spent over 40 hours to practice the algorithm. At that time, I thought that I was the expert on the algorithm. It was so naive and here is the blog link for those practices.

Code practice


I spent less than 10 minutes to analyze the algorithm, and then the peer asked me if I can improve the efficiency to get available numbers to replace '.' char. So I plan to use a hashset to store available numbers.

After I finished the code, I ran the test cases, I only passed one of test cases. So I found out that the hashset should be digits from 1 to 9 excluding those number showing in the same row or column or 3 x 3 small matrix. I just naively got those numbers and put into the hashset instead.

Here is the code. It took me exactly 30 minutes to pass all test cases. I spent around 5 minutes to fix all bugs.


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.


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.

Wednesday, September 6, 2017

Leetcode 37: Sudoku solver

Sept. 6, 2017


Introduction



It is so enjoyable to write sudoku solver as a depth first solution and also walk through each step called "thinking out loud". The conversation after the mocking this evening starting from 10:00 pm until 11:30 pm is also rewarding experience, I try to contribute my two cents for a young person to prepare for an important trial. Although I have written sudoku solver solution over 5 times last 3 months, I still found that this time I wrote a different style.


Algorithm practice 


Here is my C# practice. No bug in the first writing, only fix two compiler errors, pass all test cases the first time.

I forgot that I need to visit current node instead of using two loops to scan the matrix to find the first empty element. After 1 - 2 minutes, I noticed the issue, and then I corrected my thought process and continue to write from line 25.

I wrote line 38 and thought out loud saying that it is kind of stupid to write '1', '2' to '9', actually I am thinking that it is better to split from string "123456789" instead of declaring a char array.

After the mocking interview 


I spent over 20 minutes to share my past 80 interviews with the peer, what I have learned through the mocking practice, specially how I learn to write sudoku algorithm through more than three practices.

One thing I like to say is that if you take it slow, you have a lot of time to write code in 30 minutes mocking interview; but if you write in a rush, then you end up with issues. I am getting more comfortable to stay on one node at a time.

The fact is that in my solution there is no need to write a double for loop at all. Avoid scan from top to down by row, left to right by column to find the first empty element all the time.

The peer gave me the feedback saying that I did very good to explain the algorithm by going over the example to find options 1, 2, 4, 7 ( see line 117 - line 124).

Tuesday, May 23, 2017

Leetcode 37: Sudoku Solver

May 23, 2017

Introduction


Sudoku solver is the most classical algorithm to apply Depth First Search (DFS) using recursion and also use back tracking as well. Julia had a very good experience in May 22, 2017, she had chance to learn to write a short version of depth first search coached by her mocking peer.

Most important design is about base case. To make the implementation as simple as possible, DFS algorithm can start from the matrix left top corner, and then scan from left to right, top to bottom, if the row is bigger than 8, then it finds a solution.


Algorithm Study 



Problem statement is here.

The C# code practice is here.

Algorithm talk 


May 25, 2017

It is also very good experience to use Sudoku Solver algorithm to conduct mocking experience. Julia did her first mocking experience using the algorithm as an interviewer.

June 13, 2017

It is very good experience to write sudoku in mocking experience, this is the third time Julia worked on Sudoku for mocking experience.

Julia put the code into Visual Studio to compile and here is her C# practice code.

Here are two mistakes in writing through mocking experience:

One compiler error in mocking writing, line 61,
add type conversion by adding (char), and also (number + '0')

line 61  board[row, col] = (char)(number + '0'); // add '0'

One compiler error, duplicate variable inside for loop on line 82, change col to column; line 91, row is changed to rowIndex.

Try to pass online judge, and then find the bug on line 84, 93, 111. C# code is here.

line 84 if (board[row, column] - number == '0') // should be '0', not 0



Actionable Item



Julia still remembered that she got some great advice on her second mocking experience using Sudoku, and then she was coached by the peer to use DFS starting from (0, 0) and set the base case to row > 8.

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.




Saturday, May 21, 2016

Leetcode 37: Sudoku solver - a warm up practice

May 21, 2016

Introduction


It is time to warm up Leetcode 37: Sudoku solver algorithm. The problem statement is here. I reviewed the blog written in July, 2015 first, the blog link is here.

I also like to review one blog written by fightforyourdream, the link is here.

Analysis about Sudoku solver in Chinese


I like to translate the analysis from Chinese to English. The analysis from the above Chinese blog:

典型 DFS/递归/回溯/深搜题。对于 DFS,说白了

1. 什么时候返回?
在本题中,
    1. 当 x > 8 或 y > 8 表示已经遍历完所有的格子,因此成功完成,返回 true。
    2. 当下一个搜索(子搜索)返回true,说明已经找到,返回 true。  
    3. 如果测试过本轮的所有可能解,但无一是对的,说明无解,返回false。  
    4. 如果当前空格不是空格,则改变x,y坐标后,继续下一个空格的尝试

2)DFS 就是针对本轮的所有可能解进行逐一尝试,找到本轮的一个可能解后,这时要调用递归,基于本轮的解对下一轮(子问题)进行求解。如果下一轮(子问题)求解成功,则说明大功告成,及时返回true,停止之后的尝试。
否则如果下一轮(子问题)求解失败,则说明本轮的解不适合子问题,因此,必须换一个本轮的解,然后基于本轮的新解,继续尝试子问题。如果已经本轮所有的解都尝试过了,也都失败了,说明本问题无解,返回false。

当然在每次尝试子问题前和如果失败返回后,都要恢复原来的环境(撤销动作)。

所以,要想使 DFS 成功返回, 条件就是找到满足本轮的解和这个解也要满足下一轮(子问题)


Analysis in English 


Plan to write down English translation on Nov. 3, 2017.


Warm up practice


May 21, 2016

Try to finish in 30 minutes, here is Julia's C# practice, based on past practice - C# code