What's the distinction between those? He isn't asking the candidates to invent algorithms, just to implement simple ones. The answer to the second question is "return Pi * radius * radius", isn't that just writing code?
What's the distinction between those? He isn't asking the candidates to invent algorithms, just to implement simple ones. The answer to the second question is "return Pi * radius * radius", isn't that just writing code?
Given that Pi can be estimated using the function [...], write a function that calculates Pi to an accuracy of 5 decimal places.
...my first inclination was:
public double answer() {
return 3.14159;
}
It passes the stated requirements.As soon as 4 * 1/(2n+1) < 0.00001, then you can stop looping. The sequence is the algorithm.
while round(oldpi,5) != round(pi,5)
as your loop.
That the error must be less than the nth term does follow from 1) the fact that he says "more terms giving greater accuracy", which means the error must be monotonic, and 2) the fact that the sequence is alternating. Together, this means the two must be bouncing around the answer instead of inching toward it, and so the sum of the tail must be less than the current increment.
It won't work, however, for the general sequence (or even the general convergent sequence).
It's only the fact that it's alternating that makes it summable. The alternating series converges like 1/n^2.
This is the kind of thing that, if it's noted in an interview situation, marks a better than average candidate. It could also allow such a candidate to go on about problems with summing series, and really show off. (One secret to interviewing well being to make the given question into a question you know something about already.)
a) the numbers alternate between higher than pi and lower than pi
b) if you have a number between, say, 41 and 48 then the answer must be forty something and therefore start with a 4.
No more than 2 minutes insight really required! :)
I didn't say "invent" algorithms, I said write them. If I didn't happen to have that "simple" algorithm in my head already then it would not necessarily be trivial to implement it. Especially if it's been a while since you've worked with similar equations.
Now, if the goal is to see how the person would try to work it out allowing for questions to take the place of research then I could possibly see the benefit.
If you're given the outline for an algorithm, and asked to write code to implement it, is that writing algorithms or writing code?
The first one requires an ability, even if minor, beyond just coding while the second is all about coding. I also find it interesting that he says the second version was more difficult for his applicants than the first. Maybe instead of "algorithm" it's more about pattern recognition that leads to minor math skills to then coding.