It is my algorithm called largest smaller binary search tree key. I have practiced the algorithm over twenty times. What I have learned is that there are so many ideas to work on the algorithm, I should open to new ideas as long as it makes sense. What I have to do is to understand how good I can do to solve this algorithm over twenty times practice in last 12 months.
I still had some difficult time to figure out while loop, what I should loop on. I need to make sure that the search is going to left if the value is bigger than given number, and the search is going to right if the value is smaller than given number. I just quickly apply a test case using given number 17 and root node 20, I like the loop continues as long as possible.
Here is my C# solution.
I was also very honestly exchanging ideas with the peer how to handle this case if I know the algorithm very well, practice over 20 times recently. He smiled and then he said that it is ok. The interviewer will try to come out the extended algorithm to find out if you are memorizing the solution or not.
I explained to the peer that I already go through the stage to try to memorize the algorithm. When I work on the algorithm, if I do not write down keywords, ask, constraints, I do not write down a list of requirements, I made a few mistakes to mix one algorithm with the other in mock interview platform. I learn from the mistake and know how to play with memorization. Follow the ritural, and then give the first 5 minutes to write down the requirement, and then write down the algorithm afterwards.
I try to work hard to avoid this kind of mistakes.
This time I did on purpose to show the peer how I work on white board testing and make sure that each line of code is correct. No spell error, no syntax error, no logic problem.