Thursday, December 28, 2017

Every algorithm there is a story about hard working programmer

Dec. 28, 2017

Introduction


It is another normal mock interview practice Dec. 27, 2017 10:00 PM. My task is to write another binary search algorithm called root of a number. I have worked on binary search algorithm over 30 times last 9 months, I have wrote so many blogs about the algorithm as well. But this time I still made a mistake in the edge case, while (start < end) should be while( start <= end), and the peer was very smart to tell me that if x = 1 then my code will return -1 instead of 1. The peer was surprised that I found the bug missing = sign in less than 2 minutes before. I did not tell him that I recently had same issue and wrote a blog about the case as well.

Here is my C# code on this practice.


Binary search algorithm


The peer asked me in the mock interview a question, "how come you are so good at binary search algorithm?" I told him that I have worked on binary search algorithm last 6 months over 30 times. I have mock interviewed the peer using binary search algorithm over a few times. I made a few mistakes before, and one time the peer advised me to focus on middle, do not work on start or end edge case.

I still remembered that my last practice on this number I did wrong on the binary search range, but the mock interview platform does not catch the error.


Newton method


The peer shared me his Newton solution to solve the root of number. Here is the code.

Recursive algorithm


I spent extra 30 minutes to use ShareCode to discuss with the peer, I like to use my favorite algorithm Binary Search Tree Inorder Successor to test how good he is in terms of confidence to solve the recursive algorithm.






No comments:

Post a Comment