Introduction
I like to learn to be a good code reviewer. Sometimes I practice with a friend a few times, I start to learn how to help the peer to give some code review.
Code review
I will document my code review here as well.
Here is the Java code I got from the peer and then I cleaned up test cases and analysis of the algorithm.
Here is my code review:
isUnivalTree function
You can simplify the function isUnivalTree using one statement, please see attached image of the highlight of your code:
Here is my advise how to write in one statement instead. I will explain why I like to do that.
The argument is this, left && right is not true, you do not need to check root value compared to left child value if there is one. So your logic includes redundant work.
1. First the return type does not match API definition, people will surprise to get a return Node when the function name is called checkSize. You should return size instead of a Node. Why we need a Node, where to find the size in the Node object.
Also the function name checkSize is not meaningful, it is better to call calculateUnivalSubtreeCount.
2. Second, you have a giant expression I circled using red color. It is better to avoid negative checking. Think about default value, and then write a positive checking.
Here is the code I think with better presentation:
Clean up is a small good thing to do
I also like to point out that it is good habit to clean up document before sharing with others. Remove extra space, unrelated comment, and make the code clean and readable. Once you do it in every mock interview, you will start to learn how to act quickly. You will apply the habit to the work and also in the official interview.
Working hard does not mean staying up 1 AM or 2 AM. It takes time to build up good knowledge of data structure and algorithm. But builing a good habit just takes determination and good mind of making smart choice, that is my thinking. Hopefully I can bring your attention to the issue.
No comments:
Post a Comment