Showing posts with label N-th root of a number. Show all posts
Showing posts with label N-th root of a number. Show all posts

Wednesday, March 28, 2018

Being an interviewer: Root of a number

March 28, 2018

Introduction


It is a binary search algorithm called root of a number. The hint I gave in the mock interview as an interviewer is to explain how many numbers to search for x = 8, n = 3, it is from 0, incremented by 0.001 to 8, total is 8000 numbers to search.

Binary search algorithm


Here is the binary search algorithm I reviewed written by the peer. The algorithm still has issues to pass a few test cases. I like to look into as well.

I like to get organized and review all my past practice.

Code review


Actually the code should be updated in two places:
1. Line 44 and 45, return (double) m/ 1000;
2. Line 56, return (double) s/ 1000;

The argument is that when s == e on line 37, the return value should be s, not -1 or 0.0.

Incremental value 


It is better to change the design, and use 0.0001 as a different number to apply binary search. Here is C# code.

Given the example x = 8, n = 3, instead of search 8000 numbers, we choose to search 80,000 using binary search. Incremental value is 0.0001 instead of 0.001.

Being an interviewer, it takes some time to figure out how to guide the peer to lead the optimal solution and pass all test cases.

Sunday, January 14, 2018

Root of a number

January 14, 2018

Introduction


It is such a great machine learning algorithm on mock interview platform. I spent 50 minutes with a top ranking university with highest GPA, a master student to work on a problem solving. Quickly in less than 20 minutes the solution was written and also pass almost all test cases. I was amazed by the peer the engineering power, the way he tested the code, and the quick he applied to small increment value from 0.001 to 0.0009 to 0.0001, and also the answer he gave to me why it is good idea to always increment or decrement one value.

I have worked on the algorithm through mock interview near 10 times, I never came cross this idea to apply increment/ decrement the middle value one but just simply apply the value from 0.001 to 0.0001.


Code review


Here is the code written by the peer using C++. I like to code review later on.


Tuesday, December 12, 2017

Binary search practice

Dec. 12, 2017


Introduction


Binary search algorithm is getting easy to write and I wrote one more time on Dec. 11 10:00 PM mock interview. I wrote the algorithm using C# language and the code passes all test cases.

Code review


Here is C# code.

Line 50: return binarySearch(x, n start, end);

I forgot to write a recursive call as line 50 shows. After the whiteboard testing, I wrote the test case Root(7, 3) and then I found out the bug.


Follow up 

Dec. 19, 2017  10:14 PM

Binary search range should be discussed based on x value, if x > 1, then the search range is [0, x]. But if x < 1, then search range is [x, 1] instead.

The mock interview practice has a bug related to range to search.



Monday, June 26, 2017

N-th root of a number

June 26, 2017

Introduction 


Plan to work on N-th root of a number on geeksforgeeks.com.

Algorithm practice 



May 3, practice is here

May 30, C# practice is here. Root a number by power of n, for example, 0.001 - n = 3, root number is 0.1. 

June 26, C# practice is here

Julia did white boarding test on her own code, and then she found a bug on line 29 on test case 0.001, n = 3, search value 0.1. Julia tested her own code very carefully, and then find a bug on line 29. The peer asked the question to point out the bug on line 75, return integerValue/ 1000.0; not integerValue/ 1.0. At the end, there is a dead loop, so Julia put some debug code and traced down the bug on line 74, type conversion bug.