Introduction
I learn from my own mock interview experience. After the mock interview on Oct. 15, 2018, I got the complaint from the interviewee. So I decided to spend extra 30 minutes to help and have discussion with the interviewee.
I had chance to work with the interviewee this time, and then we had good discussion how to solve those algorithms.
How to explain the approach?
Here are the hints I gave in the mock interview. The algorithm is called longest univalue path on Leetcode.com, easy level tree algorithm.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Leetcode: longest univalue path | |
hint: | |
instead of working on longest path directly, kind of greedy, break into two parts. | |
length1 = one is the root to leaf node longest path -> left side | |
length2 = one is the root to leaf node longest path -> right side | |
path cross the root = length1 + length2 | |
In order to calculate length1 or length2, recursive can be used to calculate. Brute force all paths | |
from the tree, any node to any node, n nodes in the tree, n * (n-1) = O(n*n). It is hard to compute. | |
Instead brute force every node as root node of the tree, determine the longest path cross the root node. | |
More hint: | |
work on those simple trees: | |
4 4 4 4 | |
/ / \ / \ | |
4 4 4 4 4 | |
/ | |
4 | |
Recursive function: | |
return 0, 1, 1, 2 | |
longest path: | |
return 0, 1, 2, 3 | |
My advice
I also wrote down my advice after the mock interview as the interviewer.
No comments:
Post a Comment