by droithomme · 14y ago · 0 comments
It's weird there are so many companies claiming to not be able to find people but you hear about their hiring process and it's no wonder.
For example, everybody knows merge sort, but assuming they didn't, draw out a couple iterations in a graph and ask them to code it.
(I don't think it's an awesome interview question in practice though, too many people do already know how to do it.).
And again, I do not think this is a good interview question. I just think it's bad for a different reason than the original poster claimed. Which was that it's supposedly not solvable even by top engineers unless they've recently implemented the algorithm.
def sqrt(x):
a = 1.0
for _ in range(100): a = (a + x/a)/2
return a
This is just Newton's method to find a solution of the equation a^2 - x = 0. For certain classes of jobs knowing Newton's method is definitely required, as it is the simplest and most widely used numerical method. And even if you don't know Newton's method I expect any programmer worth his salt to be able to come up with a bisection algorithm.I've written 100,000s lines of code and released 9 different commercial products over the last 15 years, and Newton's method is far from being in my working memory.
Now, if you ask about best practices in shipping software...
That said, Newton's method is not rocket science. The basic algorithm is iterating this line:
z = z - f(z)/f'(z)
Where f is some function and f' is its derivative. In many cases this will rapidly converge to a root of f (i.e. z will converge such that f(z) = 0). As far as algorithms go, this one is pretty damn simple. It is not in my working memory either, and I expect that many people rederive it when they need it.The idea behind Newton's algorithm is that you approximate the function f(x) with the tangent line L at z:
L(x) = A + B*(x-z)
with A = f(z) and B = f'(z)
Now L(x) is an approximation of f(x). How well an approximation this is depends on how much f(x) looked like a line in the first place. Then instead of solving f(x) = 0 (which is our goal, but hard) you solve L(x) = 0 (which is not exactly our goal but almost our goal, and it is easy). So: f(z) + f'(z)(x-z) = 0
Rewriting: x = z - f(z)/f'(z)
Now the approximation of f(x) at that x is 0. Because it's only an approximation to f(x) it will probably not make the real f(x) = 0. Therefore we apply the method again and again with x as the new starting point: z = some starting value
until f(z) is small enough:
z = z - f(z)/f'(z)
Because we're iterating, this method works even if f(x) doesn't look much like a line. For example x^2 - a doesn't look much like a line, but Newton's method is very effective at computing its root to compute sqrt. To see how outrageously effective it is, lets compute sqrt(2.0) with it. Lets start with a starting guess x = 1.0. We get the following numbers: iteration x
--------- -
1 1.0
2 1.5
3 1.416666666666666
4 1.414215686274509
5 1.414213562374689
6 1.414213562373095
7 1.414213562373095
Note that at the 6th iteration already all 16 digits are correct.Newton's method is perhaps the most beautiful algorithm due to its simplicity and effectiveness, and worth knowing even if only for its beauty. It is also quite probably the most important algorithm ever invented. It is used everywhere from division in hardware to optimizing all kinds of things and solving differential equations.
It's a fairly straight-forward transformation.
No. Not really. During a test I quickly coded the solution to a numeric problem in BASIC on a Casio PB-700. It was fun to watch the HP-41 crowd tearing their hair off. That's when I first learned pg's "beating the averages" lesson.
They need us so badly that they grasp at anything to show us we dont deserve to be paid. Its economics.
This seems more plausible to me. Recruiting is hard.
DELTA = 0.0000001
def sqrt_bin_search(squared_value, lower, higher)
middle = (lower + higher) / 2.0
if ((middle * middle) - squared_value).abs < DELTA
middle
elsif middle * middle > squared_value
sqrt_bin_search(squared_value, lower, middle)
else
sqrt_bin_search(squared_value, middle, higher)
end
end
def sqrt(x)
sqrt_bin_search(x, 0, x)
end