I prefer to have about 15-20 index cards with problems that have open ended solutions ("Build a RPG combat system using 4,6,8,10, 12 or 20 sided dice", "Create a system to store Books and allow people to search by author/title or genre")
I prefer to have about 15-20 index cards with problems that have open ended solutions ("Build a RPG combat system using 4,6,8,10, 12 or 20 sided dice", "Create a system to store Books and allow people to search by author/title or genre")
If they insist that it must be solved at runtime, I would check the size of the computed term (1/(1+2x)) and repeat 'til it's less than 10E-6.
That's a lot of iterations.
p(n) = p(n-1) + 4*((-1)^(n-1))/(2n-1)
n = {1,2,3...} and p(0)=0
I think that p(n)-p(n-1) must be less than the precision you want (0.00001).*Mental note: never start reply math on iPad ;)
My larger conclusion is that either the OP didn't intend to require this analysis or he's surprised that people in an interview+coding mindset are going to get stuck thinking there is a straightforward algorithm. Since he refers to the question as simple for any "seasoned developer" (read: has forgotten algebra ;), I presume he actually misstated the problem and really expected one of the simplifications people have come up with. Either way, I think it says more about the interviewer than the interviewees.
... although maybe my gripe only came about from knowing enough to immediately see that there had to be some kind of convergence bound, but not knowing/figuring exactly what it was.
iterate and alternately subtract and add 1/i, (then i=i+2) to get your multiplier.
Each iteration multiply 4 times the resulting multiplier. Break when the answer rounded to 5 decimal places == 3.14159
>Given the area of a circle is given by Pi times the radius squared, write a function to calculate the area of a circle.
I think we can assume he was just looking for something trivial.
Also, his next step down isn't really one jump down in difficulty; it's presented as "if you don't even know how to begin to approach that problem I need to make sure that you can code something trivial." That is to say, it's an arbitrary number of steps down that implies little about the initial question's intended difficulty.
You could be right though, in that the high number of developers who failed it freaked out at the site of pi and flubbed it without really thinking further. Although I think that says more about our horrible math education than anything else, i.e., math education is so bad that the mere site of something "mathy" elicits terror in the hearts of otherwise competent programmers.
A solution to this is looking at the distance from the current sum to the "boundary":
abs(round(sum, 5) - sum) > next_term
which works because this is an alternating series.I guess that demonstrates why it's important to have a REPL or a compiler available for the interviewee. (My computer wasn't available at the time that I wrote the comment.) I just wrote up my solution and confirmed that its terminating condition isn't correct.
There's a mindboggling amount of people applying for programming jobs who can't even do that!
I have been a code producing developer for more than 40 years. I hate wasting time proving that I can do trivial problems. Sometimes I have spent half an afternoon writing one trivial problem after another. I would much rather spend the time looking at the company's problems and discussing solutions.
Of course, it's actually trickier than you might think on modern computers if you go out much further than 10 decimal places or so.