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

No comments:

Post a Comment