Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I agree with #18 wholeheartedly but I disagree with the examples because those are more of math questions than programming questions. I have never needed to write a program that dealt with circles/radius/area/pi so I'd probably fail that test as I'd have to look up a few things that I have't thought about in over twenty years.

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 you can't answer those questions in #18, and you can code, then it isn't mathematical skills you lack - it's reading comprehension (a skill that seems to be woefully underdeveloped amongst developers). I suspect you can actually pass both of those without difficulty though.


I have to agree. The formula is given right there in the problem description. This isn't about knowing math. It's about whether you can translate some simple text, "Pi times the radius squared," into code. This particular example is trivially easy; even easier than FizzBuzz.


What's the easy answer to the first one? It seems that you need to analytically prove a bound for the remaining terms, no?


I would just write the code to compute it to arbitrary precision and then tell the interviewer that I would test how many iterations are required and hard-code it.

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.


If you're hardcoding it, n will be around 400,000.

That's a lot of iterations.


Besides the slowness of such a solution, that condition is not enough to ensure the first five decimal places are correct.


If you are told the formula to use, you use it. No, it's not the bestest way ever of finding pi. It's to make sure the guy sitting across from you can actually write code, which a scary number of people actually cannot do.


The formula is

  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 ;)


It's an alternating decreasing series so the sum of the remaining terms is always less than the current term. Also, each pair is 1/n - 1/(n+2) = 2/(n*(n+2)) which converges like 1/n^2 (all successive terms sum to the same order as the current term). That this pairing converges much faster than the original series is part of what makes the question good: a good solution will exploit this pairing for numerical stability and to converge in much fewer steps (square root of the number needed by the naive algorithm).


I was thinking that you'd want to pair the terms, but going further didn't happen to tickle my curiosity. Of course after seeing your answer, it looks quite obvious.

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.


The interviewer can ask leading questions if the interviewee gets stuck or doesn't consider certain attributes. Seemingly simple questions that actually have depth are good for an interview because it stimulates discussion and can be carried further for more advanced/competent people. Quizzing someone on "trivia" is a terrible test of competence.


Yeah, it's true that good technical questions are an interactive process. But I'm not too confident that the OP actually saw the complexity in that question, and the many naive responses certainly did not. Also in the context of trying to come up with code on the spot, knowing how that infinite sum is going to converge strikes me as a quite helpful piece of trivia.

... 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.


I think the interviewer was asking for something really easy and un-optimized. Something like:

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


I wonder if they really do mean to break when the answer hits the known value. If so that's kind of a pathetically useless thing to do. While I know most whiteboard coding problems aren't things you ever need to write in production code, the change to make it break when five decimal places stabilise is not much more difficult and provides an actual nontrivial output.


given that his next step down in difficulty was

>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.


I'm sure it would be fine as a first pass but there's no way anyone would end it there. Two obvious immediate objections being that the point where it first rounds to the correct value does not imply the point where it stabilises there and the second being that you're using the value you're trying to calculate in calculating said value. At that point, a good interviewer leads them toward fixing these issues rather than just saying "wrong", of course, but I'm sure the end result is not to use pi in the algorithm.

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.


The only part of the question that required any math knowledge beyond basic arithmetic was knowing the value of pi to 5 decimal places. I'm sure they wouldn't have minded if you'd asked what exactly that was.

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.


You don't even need to know that. Once the fractions update your estimate by less than 0.5x10^-5 (half of 0.00001), you can be sure that your estimate is accurate to 5 decimal places. I think it's reasonable to expect a programmer to be able to figure that out. If the programmer wants to use a REPL to find the pattern in the numbers, I think that's fair as it demonstrates using one's tools to solve a problem.


That's not a correct solution. Try using that solution to find pi to 2 decimal places (delta of 0.005) and you'll wind up with 3.139..

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.


You're right. Thanks for the correction.

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.


It doesn't matter that it's not correct, the sad truth is that if you get that problem and write a loop and some math operators and update a variable for each run - you're in the top 10% of applicants!

There's a mindboggling amount of people applying for programming jobs who can't even do that!


How can people code without knowing math ? Atleast to the extent of knowing the value of PI? ( I am not asking for a 10 decimal places value ..How about just 2 decimal places )


I'm with you there. I didn't mean to imply that programmers don't need to know math. To the contrary, I find it very useful and keep a graphing calculator on my desk. I was just pointing out that knowing the value of PI isn't strictly necessary for this example, because by writing a quick script, one can determine the behavior of the sequence and figure out an appropriate stopping condition. The rest of it is just translating a formula into code.


How many other constants have you memorized? e? phi? sqrt(2)? and you know all those to 5 decimals? When I conduct interviews I try really really hard to set aside the things that I know and give candidates an opportunity to tell (or show) me what they know. Someone not having a constant memorized is not a show stopper, as I am after the logical thought processs.


return 3.14159;

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.


If you cannot read and write a trivial math equation as code then I really question how you got here. That formulation for approximation of irrationals is one of the reasons computers exist! If you can't do this, go into a cave on a mountaintop behind a waterfall in a hidden vale and proceed to train until you can. It's not an unreasonable or un-useful thing.

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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: